Regex: Matching a space-joined list of words, excluding last whitespace

Posted by Jesper on Stack Overflow See other posts from Stack Overflow or by Jesper
Published on 2010-04-23T14:33:18Z Indexed on 2010/04/23 14:43 UTC
Read the original article Hit count: 374

Filed under:
|

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>(\w+\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' => {'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'}

© Stack Overflow or respective owner

Related posts about python

Related posts about regex