使用正则表达式搜索分隔符

时间:2015-05-22 18:36:28

标签: regex python-2.7

在下面的字符串中,我需要将re.findall用于'(任何一天)'然后将其打印到分隔符','previous

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)

我出错的任何帮助或建议。

1 个答案:

答案 0 :(得分:0)

你几乎是正确的

您需要做的就是将字符类更改为否定为

r'[^,]+\(any\s\bday\)'
  • [^,]+否定字符类,匹配,以外的任何内容,直到找到any day

  • 输入为单行

    时,可以删除
  • re.M

<强>测试

>>> 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)']
相关问题