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
_______________________________________________ Pythonmac-SIG maillist - Pythonmac-SIG@python.org http://mail.python.org/mailman/listinfo/pythonmac-sig