Regex 使用正则表达式搜索到分隔符
在下面的字符串中,我需要将re.findall用于“(任意一天)”,然后将其打印到分隔符“,”之前Regex 使用正则表达式搜索到分隔符,regex,python-2.7,Regex,Python 2.7,在下面的字符串中,我需要将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:0
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)
未按预期工作:
re.findall(r'[^\s,]+Weekend\s\bonly' ,rr, re.M)
re.findall(r'[\s,]+\(any\s\bday\)' ,rr, re.M)
任何帮助或建议我哪里出了问题。你几乎是对的 您所需要做的就是将character类更改为negation
r'[^,]+\(any\s\bday\)'
否定字符类,匹配除[^,]+
以外的任何字符,直到找到,
任何一天
可以删除,因为输入是单行的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)']
多行
模式修饰符也不需要。@hwnd我同意你的看法。从op的代码中复制。现在在答案中更正。谢谢你是的re.M是不需要的,我有很多其他文件,我正在使用这个sting来获取数据。完全同意此处不需要多行