Reputation: 481
I would like to match the values between a given delimiter using a Regex expression in Python. I would like this to ignore surrounding white space. For example:
The string 1, b cc, "a"
and delimiter ,
will return three matches of 1
,b cc
,"a"
The string 4 + 5 + 2 +1
and delimiter +
will return four matches of 4
,5
,2
,1
Upvotes: 1
Views: 1004
Reputation: 2015
import re
line = '1, b cc, "a"'
re.split(r'[;,]\s*', line)
Out[7]: ['1', 'b cc', '"a"']
line = '4 + 5 + 2 +1'
re.split(r'\s*[+]\s*', line)
Out[10]: ['4', '5', '2', '1']
The re.split()
function is useful because you can specify multiple patterns for the separator.
In this case, for your first request, the separator is either a comma (,)
, semicolon (;)
, followed by any amount of extra whitespace
. For your second request, the separator is plus (+)
, surrounded by any amount of extra whitespaces
.
Whenever that pattern is found, the entire match becomes the delimiter between whatever fields lie on either side of the match. The result is a list of fields, just as with str.split()
Upvotes: 2
Reputation: 1406
You can do this with the re.split()
method.
import re
re.split('\s*,\s*', '1, b cc, "a"')
re.split('\s*\+\s*', '4 + 5 + 2 +1')
Upvotes: 2