[Pythonmac-SIG] RegEx 101 (or 102?)
Charles Hartman
charles.hartman at conncoll.edu
Thu Jan 13 13:55:45 CET 2005
Not quite Python, not quite Mac -- but I'm stalled on something about
REs that I ought to know but can't seem to find.
I want to search a string like '/xx/x/x/xx' for the *longest* match of
'x[x/]' -- that is, the longest sequence of pairs whose first member is
'x' and whose second member is either 'x' or '/'. The obvious solution
is
for grp in sre.finditer( '(x[x/])+' , str):
if <grp.end()-grp.start() is longer than longest previously examined>
<grp is the new longest>
But this doesn't work, and every approach I've thought of seems to have
the same problem: Searching
/ x x / x / x / x x
the pattern first finds the 'xx' beginning at position 1, and then the
'x/x/xx' beginning in position 4. But it doesn't find the 'x/x/x/xx'
beginning at position 2. I guess it starts looking for the next group
after, not in the middle of, the group it's already found. The
solutions that occur to me are pretty Rube Goldberg (run the search
multiple times, cutting one character off the beginning of the string
each time . . .) I know there's a better solution.
Charles Hartman
More information about the Pythonmac-SIG
mailing list