Match First Sequence in Regular Expression?
Roger L. Cauvin
roger at deadspam.com
Thu Jan 26 18:11:39 CET 2006
"Christos Georgiou" <tzot at sil-tec.gr> wrote in message
news:t9uht1l1c37c6plgvc7senk1h6tnktg419 at 4ax.com...
> On Thu, 26 Jan 2006 16:26:57 GMT, rumours say that "Roger L. Cauvin"
> <roger at deadspam.com> might have written:
>>"Christos Georgiou" <tzot at sil-tec.gr> wrote in message
>>news:boqht19rs7946mtk5s64hqrieq44he5aq7 at 4ax.com...
>>> On Thu, 26 Jan 2006 14:09:54 GMT, rumours say that "Roger L. Cauvin"
>>> <roger at deadspam.com> might have written:
>>>>Say I have some string that begins with an arbitrary sequence of
>>>>and then alternates repeating the letters 'a' and 'b' any number of
>>>>I'm looking for a regular expression that matches the first, and only
>>>>first, sequence of the letter 'a', and only if the length of the
>>>>Does such a regular expression exist? If so, any ideas as to what it
>>> Is this what you mean?
>>Close, but the pattern should allow "arbitrary sequence of characters"
>>precede the alternating a's and b's to contain the letter 'a'. In other
>>words, the pattern should accept:
>>since the 'a' between the 'x' and 'y' is not directly followed by a 'b'.
>>Your proposed pattern rejects this string.
> This finds the first (leftmost) "aaa" either at the end of the string or
> followed by 'b' and then arbitrary sequences of 'a' and 'b'.
> This will also match "aaaa" (from second position on).
> If you insist in only three 'a's and you can add the constraint that:
> * let s be the "arbitrary sequence of characters" at the start of your
> searched text
> * len(s) >= 1 and not s.endswith('a')
> then you'll have this reg.ex.
> If you want to allow for a possible empty "arbitrary sequence of
> at the start and you don't mind search speed
> This should cover you:
>>>> m= r.match(s)
Thanks for continuing to follow up, Christos. Please see my reply to your
other post (in which you applied the test cases).
Roger L. Cauvin
nospam_roger at cauvin.org (omit the "nospam_" part)
Product Management / Market Research
More information about the Python-list