2015-05-22 56 views
0

在下面的字符串,我需要使用re.findall为“(任何一天)”,那么它打印高达分隔符“”前搜索高达分隔符使用正则表达式

rr='PU3lserver1^server2|ABAP|Revisions|true|null|Weekend 
only,ATN|server3|ABAP|Revisions|true|null|1:00 AM to 3:00 AM CET (any 
day),B4P|server4^server5|ABAP|Revisions|true|Generic AFL|8:00 PM to 3:00 AM 
CET (any day),C8B|server6|ABAP|Revisions|true|Generic AFL|8:00 PM to 3:00 AM 
CET (any day),QU8|testserver|ABAP|Revisions|true|null|1:00 AM to 3:00 AM CET 
(any day),S77|testserver|ABAP|Revisions|true|null|Weekend only' 

作品以及预期:

re.findall(r'[^\s,]+Weekend\s\bonly' ,rr, re.M) 

不能按预期工作:

re.findall(r'[\s,]+\(any\s\bday\)' ,rr, re.M) 

任何帮助或建议,我要去的地方错了。

回答

0

你几乎是正确

所有你需要做的是改变字符类的否定作为

r'[^,]+\(any\s\bday\)' 
  • [^,]+否定的字符类,匹配比,直到any day其他任何被发现

  • re.M可以作为输入是单林ED

测试

>>> re.findall(r'[^,]+\(any\s\bday\)' ,rr) 
['B4P|server4^server5|ABAP|Revisions|true|Generic AFL|8:00 PM to 3:00 AM \nCET (any day)', 
'C8B|server6|ABAP|Revisions|true|Generic AFL|8:00 PM to 3:00 AM \nCET (any day)', 
'QU8|testserver|ABAP|Revisions|true|null|1:00 AM to 3:00 AM CET \n(any day)'] 
+1

是不需要的'多line'模式改性剂任一。 – hwnd

+0

@hwnd我同意你的意见。从op的代码复制。现在更正了答案。谢谢 – nu11p01n73R

+0

是的,不需要,我有很多其他的文件,我正在使用这个sting来获取数据。 完全同意此处不需要的多行 – Deepak