Skip to content
Advertisement

combining split with findall

I’m splitting a string with some separator, but want the separator matches as well:

JavaScript

I can’t find an easy way to combine the 2 lists I get:

JavaScript

Into the desired output:

JavaScript

Advertisement

Answer

From the re.split docs:

If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list.

So just wrap your regex in a capturing group:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement