Regex: Matching a space-joined list of words, excluding last whitespace
How would I match a space separated list of words followed by whitespace and some optional numbers? I have this:
>>> import re
>>> m = re.match('(?P<words>(\S+\s+)+)(?P<num>\d+)?\r\n', 'Foo Bar 12345\r\n')
>>> m.groupdict()
{'num': '12345', 'words': 'Foo Bar '}
I'd like the words group to not include the last whitespace(s) but I can't figure this one out. I could do a .strip() on the result but that's not as much fun :)
Some strings to test and wanted result:
'Foo & Bar 555\r\n' =开发者_JAVA百科> {'num': '555', 'words': 'Foo & Bar'}
'Hello World\r\n' => {'num': None, 'words': 'Hello World'}
'Spam 99\r\n' => {'num': 99, 'words': 'Spam'}
'Number 1 666\r\n' => {'num': 666, 'words': 'Number 1'}
I'm a bit confused by your double capturing group, and the fact that you're using \w
but want to match a non-word character like &
(maybe you mean \S
, non-spaces, where you say \w
...?), but, maybe...:
>>> import re
>>> r = re.compile(r'(?P<words>\w+(?:\s+\S+)*?)\s*(?P<num>\d+)?\r\n')
>>> for s in ('Foo & Bar 555\r\n', 'Hello World\r\n', 'Spam 99\r\n',
... 'Number 1 666\r\n'):
... print s, r.match(s).groupdict()
...
Foo & Bar 555
{'num': '555', 'words': 'Foo & Bar'}
Hello World
{'num': None, 'words': 'Hello World'}
Spam 99
{'num': '99', 'words': 'Spam'}
Number 1 666
{'num': '666', 'words': 'Number 1'}
>>>
精彩评论