Johandk
Johandk

Reputation: 428

Regex fields extraction with python

I have a string like this:

field1:value1 field2:value2

field can have spaces ie. "field name:" but the value field will never have any.

Using a regex what is an easy way extract the field value pairs into numerical groups without knowing the field name beforehand?

I'm using python b

Thanks

Upvotes: 1

Views: 5634

Answers (3)

RichieHindle
RichieHindle

Reputation: 281475

You can use re.findall() to do what you want:

>>> data = "field1:value1 field2:value2 field with space:something"
>>> re.findall(r'\s*([^:]+):(\S+)', data)
[('field1', 'value1'), ('field2', 'value2'), ('field with space', 'something')]

Upvotes: 2

user
user

Reputation: 6947

Something like this, perhaps?

([^:]+:[^ ]*)*

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336148

>>> subject = "field name 1:value1 field2:value2  field name3:value3"
>>> d = { match.group(1): match.group(2)
...       for match in re.finditer(r"([^:]+):(\S+)\s*", subject)
...     }
>>> d
{'field name 1': 'value1', 'field2': 'value2', 'field name3': 'value3'}

This is using a dictionary comprehension that's populated using this regex:

([^:]+) # one or more characters except :  (--> group(1))
:       # a literal :
(\S+)   # one or more non-whitespace characters (--> group(2))
\s*     # optional trailing whitespace (before the next match)

Upvotes: 7

Related Questions