Bryce93
Bryce93

Reputation: 481

Regex - Match each item between multiple occurrences of delimiter

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:

Upvotes: 1

Views: 1004

Answers (2)

MaThMaX
MaThMaX

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

Bo Borgerson
Bo Borgerson

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

Related Questions