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
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()
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')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.