regex question

Paul McGuire ptmcg at austin.rr.com
Fri Apr 27 09:33:58 CEST 2007


On Apr 27, 1:33 am, proctor <12cc... at gmail.com> wrote:
> hello,
>
> i have a regex:  rx_test = re.compile('/x([^x])*x/')
>
> which is part of this test program:
>
> ============
>
> import re
>
> rx_test = re.compile('/x([^x])*x/')
>
> s = '/xabcx/'
>
> if rx_test.findall(s):
>         print rx_test.findall(s)
>
> ============
>
> i expect the output to be ['abc'] however it gives me only the last
> single character in the group: ['c']
>
> C:\test>python retest.py
> ['c']
>
> can anyone point out why this is occurring?  i can capture the entire
> group by doing this:
>
> rx_test = re.compile('/x([^x]+)*x/')
> but why isn't the 'star' grabbing the whole group?  and why isn't each
> letter 'a', 'b', and 'c' present, either individually, or as a group
> (group is expected)?
>
> any clarification is appreciated!
>
> sincerely,
> proctor

As Josiah already pointed out, the * needs to be inside the grouping
parens.

Since re's do lookahead/backtracking, you can also write:

rx_test = re.compile('/x(.*?)x/')

The '?' is there to make sure the .* repetition stops at the first
occurrence of x/.

-- Paul




More information about the Python-list mailing list