In PHP one can use the function preg_match with the flag PREG_OFFSET_CAPTURE in order to search a regex patter within a string and know what follows and what comes first. For example, given the string aaa bbb ccc ddd eee fff, I’d like to match-split r'ddd' and have:
before = 'aaa bbb ccc '
match = 'ddd'
after = ' eee fff'
How to do this in python? Thanks
You can use
re.split()but you need to put parentheses around the pattern so as to save the match:but in this case you don’t need a regex at all:
Edit: I should probably also mention that with re.split you will get all of the matching groups, so you need to be prepared for that or use non-capturing groups everywhere you would otherwise use parentheses for precedence: