I have a Perl regular expression (shown here, though understanding the whole thing isn\'t hopefully necessary to answering this question) that contains the \\G metacharacter
You can use re.match to match anchored patterns. re.match will only match at the beginning (position 0) of the text, or where you specify.
def match_sequence(pattern,text,pos=0):
pat = re.compile(pattern)
match = pat.match(text,pos)
while match:
yield match
if match.end() == pos:
break # infinite loop otherwise
pos = match.end()
match = pat.match(text,pos)
This will only match pattern from the given position, and any matches that follow 0 characters after.
>>> for match in match_sequence(r'[^\W\d]+|\d+',"he11o world!"):
... print match.group()
...
he
11
o