Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用正则表达式搜索到分隔符_Regex_Python 2.7 - Fatal编程技术网

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

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

在下面的字符串中,我需要将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)
未按预期工作:

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来获取数据。完全同意此处不需要多行