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 nu开发者_JAVA技巧merical groups without knowing the field name beforehand?
I'm using python b
Thanks
>>> 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)
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')]
Something like this, perhaps?
([^:]+:[^ ]*)*
精彩评论