from http://docs.python.org/library/re.html#re.split
>>> re.split('(\W+)', 'Words, words, words.') ['Words', ', ', 'words', ', ', 'words', '.', '']
so your example will be
>>> re.split(r'(^a\s*)', "a foobar") ['', 'a ', 'foobar']
at this point you can separate the odd elements (your coincidence) from the even elements (the rest).
>>> l = re.split(r'(^a\s*)', "a foobar") >>> l[1::2]
This has the advantage over re.sub in that you can determine when, where and how many matches are found.
source share