Reputation: 35982
In [30]: s = '123 45$78 98@abc %'
In [31]: print re.findall(r'\b[\w]+ ?', s)
['123 ', '45', '78 ', '98', 'abc ']
In [32]: print re.findall(r'(\b[\w]+ ?)', s)
['123 ', '45', '78 ', '98', 'abc ']
In [33]: print re.findall(r'(\b[\w]+ ?)+', s)
['45', '98', 'abc ']
The expected results are as follows:
['123 45', '78 98', 'abc ']
Question> The focus of this question is not related to how to split a string. Instead, I want to know whether python allows us to do greedy matching.
Thank you
Upvotes: 0
Views: 66
Reputation: 82899
Python does greedy matching. The problem is that your regular expression seems to be wrong.
With [\w]+ ?
, you are matching some characters, optionally followed by a space. What you seem to want is just some characters or spaces mixed together. Try this:
In [10]: re.findall(r'\b[\w ]+', s)
Out[10]: ['123 45', '78 98', 'abc ']
Upvotes: 1