This is a question related to conditional regex in python:
I would like to match the string "abc" with
match(1)="a" match(2)="b" match(3)="c"
but also matches string " a" with
match(1)="a" match(2)="" match(3)=""
The following ALMOST code does this, the problem is that in the first case match(1)="a" but in the second case match(4)="a" (not match(1) as desired).
In fact, if you for g in re.search(myre,teststring2).groups(): over all groups using for g in re.search(myre,teststring2).groups(): you get 6 groups (not 3 as expected).
import re import sys teststring1 = "abc" teststring2 = " a" myre = '^(?=(\w)(\w)(\w))|(?=\s{2}(\w)()())' if re.search(myre,teststring1): print re.search(myre,teststring1).group(1) if re.search(myre,teststring2): print re.search(myre,teststring2).group(1)
Any thoughts? (note that this is for Python 2.5)
source share