See two examples how re.split() works:
>>> re.split(',', 'a,b')
['a', 'b']
but
>>> re.split('(,)', 'a,b')
['a', ',', 'b']
Why I get ',' in the list? How to avoid it?
I am asking, because I would like to make a split using an expression similar to 'xy(a|b)cd'.
Regex to Split string with multiple delimiters For example, using the regular expression re. split() method, we can split the string either by the comma or by space. With the regex split() method, you will get more flexibility.
To split a string by a regular expression, pass a regex as a parameter to the split() method, e.g. str. split(/[,. \s]/) . The split method takes a string or regular expression and splits the string based on the provided separator, into an array of substrings.
You do not only have to use literal strings for splitting strings into an array with the split method. You can use regex as breakpoints that match more characters for splitting a string.
groups() method. This method returns a tuple containing all the subgroups of the match, from 1 up to however many groups are in the pattern. The default argument is used for groups that did not participate in the match; it defaults to None. In later versions (from 1.5.
Use a non-capturing group, like:
re.split('(?:,)', 'a,b')
It works that way because it’s documented to work that way:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With