help-octave
[Top][All Lists]
Advanced

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

Re: regexp question


From: Sergei Steshenko
Subject: Re: regexp question
Date: Mon, 5 Dec 2011 13:54:50 -0800 (PST)




----- Original Message -----
> From: PhilipNienhuis <address@hidden>
> To: address@hidden
> Cc: 
> Sent: Monday, December 5, 2011 11:40 PM
> Subject: regexp question
> 
> I need to check if a string contains one or more sequences of exactly two
> identical consecutive characters.
> So I figured I could do (in Octave-3.4.3 MinGW), e.g.:
> 
> octave:1> tststr = 'baaaaa baab'
> tststr = baaaaa baab
> octave:2> regexp (tststr, "a{2}")
> 
> but the result is:
> 
> ans =
>    2   4   9
> 
> which -depending on perception- may be right but intuitively is not what I
> expected (uhm, hoped).
> I'd expect the first sequence of five consecutive "a"s to be 
> indexed only
> with a pattern of "a{5}" but obviously I'm wrong.
> 
> Now, the "aaaaa" substring actually contains 4 "aa" 
> sequences, so regexp's
> choice is at least a bit arbitrary; I couldn't find in the docs that regexp
> only returns non-overlapping substring patterns.
> IMO strfind() does this more consistently:
> 
> octave:12> strfind (tststr, 'aa')
> ans =
>    2   3   4   5   9
> 
> 
> Anyway:
> 
> Q1: Just to be sure: IS this the way regexp() is supposed to work?
> 
> Q2: Of course: how can I get together what I'm after? i.e., to find only the
> position of the one "aa" substring in tststr starting at position 9?
> 
> Thanks,
> 
> Philip
> 

I guess you need 'aa' surrounded by not 'a'. Octave uses PCRE; I am not 
familiar with nuances of Octave PCRE usage; in Perl I would write the regular 
expression this way:

[^a]aa[^a]

and if/when it matches, it returns pointer to the character preceding the 'aa' 
substring, i.e. in case of 'baab' it should return pointer to the first 'b'.

Regards,
  Sergei.

P.S. '[^a]' is so called character class - it matches all characters except for 
'a'.

P.P.S. You probably need:

http://perldoc.perl.org/perlreftut.html
http://perldoc.perl.org/perlretut.html
http://perldoc.perl.org/perlre.html


>


reply via email to

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