开发者

Python split string on regex

I'm trying to split a string using a regular expr开发者_如何转开发ession.

Friday 1Friday 11 JAN 11

The output I want to achieve is

['Friday 1', 'Friday 11', ' JAN 11']

My snippet so far is not producing the desired results:

>>> import re
>>> p = re.compile(r'(Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday)\s*\d{1,2}')
>>> filter(None, p.split('Friday 1Friday 11 JAN 11'))
['Friday', 'Friday', ' JAN 11']

What am I doing wrong with my regex?


The problem is the capturing parentheses. This syntax: (?:...) makes them non-capturing. Try:

p = re.compile(r'((?:Friday|Saturday)\s*\d{1,2})')


You can also use 're.findall' function.

\>>> val  
'Friday 1Friday 11 JAN 11 '  
\>>> pat = re.compile(r'(\w+\s*\d*)')  
\>>> m=re.findall(pat,val)  
\>>> m  
['Friday 1', 'Friday 11', 'JAN 11']


p = re.compile(r'(Friday\s\d+|Saturday)')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜