octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: regexp: returning '0' in place of 'empty' for no match ?


From: Olaf Till
Subject: Re: regexp: returning '0' in place of 'empty' for no match ?
Date: Fri, 19 Nov 2010 12:07:11 +0100
User-agent: Mutt/1.5.18 (2008-05-17)

On Fri, Nov 19, 2010 at 02:09:09AM -0800, CdeMills wrote:
> 
> Hello,
> 
> Problem: scan a number of strings for some pattern using cellfun. In its
> simplest form, regexp() returns an integer if there is a match, or 'empty'
> if there is no. So, to construct an index of the matching strings, the
> construct is rather long:
> 
> find(~cellfun('isempty', cellfun(@(x) regexp(x, my_pattern), my_strings,
> 'UniformOutput', false)))
> 
> A bit of explanation: an iteration is performed on 'my_strings', and
> UniformOutput must be set to false, otherwise the innermost cellfun will
> stop in case regexp returns empty values. The result is a cell array, on
> which a second iteration is performed to detect non-empty ones.
> 
> The construct would be simpler if regexp return value was 0 for a no-match
> -- such value can't be used as an index. Would be possible to either change
> regexp interface, which contains a risk of breaking things, either add some
> flag to force a non-empty return value ?

As for always returning zero for no match, this would make it more
difficult to get the number of matching substrings (normally
length(returned_vector)).

As for a flag, this seems a bit bizarre to me, since the returned
vector contains an index for each matching substring, so empty is
natural for no match.

BTW for a general "my_strings" you have to set "UniformOutput" anyway,
since regexp() might return a vector with more than one element.

Olaf


reply via email to

[Prev in Thread] Current Thread [Next in Thread]