Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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
Python 要匹配字符串的正则表达式_Python_Regex - Fatal编程技术网

Python 要匹配字符串的正则表达式

Python 要匹配字符串的正则表达式,python,regex,Python,Regex,请为我提供与以下字符串匹配的正则表达式 "self.unsupported_cmds = [r'\s*clns\s+routing'," 提前谢谢 我试了以下方法 re.match("^self\.unsupported\_cmds| ?=\ ?\[r\'.*\,", line) 其中line是上述字符串。它不起作用 我没有收到任何错误或异常,只是与上面给出的字符串不匹配。我不确定您的问题是什么,我认为不是正则表达式 你真的只调用这个代码吗 re.match("^self\.unsuppor

请为我提供与以下字符串匹配的正则表达式

"self.unsupported_cmds = [r'\s*clns\s+routing',"
提前谢谢

我试了以下方法

re.match("^self\.unsupported\_cmds| ?=\ ?\[r\'.*\,", line)
其中line是上述字符串。它不起作用


我没有收到任何错误或异常,只是与上面给出的字符串不匹配。

我不确定您的问题是什么,我认为不是正则表达式

你真的只调用这个代码吗

re.match("^self\.unsupported\_cmds| ?=\ ?\[r\'.*\,", line)
如果是,请尝试以下操作:

if(re.match("^self\.unsupported\_cmds| ?=\ ?\[r\'.*\,", line)):
    print "Match"
对我来说,这是一场回归赛,这意味着你的正则表达式在工作,但它是一个奇怪的正则表达式


也许你应该看一下

以下是获得它的多种方法之一:

>>> x = r'''"self.unsupported_cmds = [r'\s*clns\s+routing',"'''
>>> print x
"self.unsupported_cmds = [r'\s*clns\s+routing',"
>>> import re
>>> pattern = re.escape(x)
>>> re.match(pattern, x)
<_sre.SRE_Match object at 0x7ffca3f66098>
>>> print pattern
\"self\.unsupported\_cmds\ \=\ \[r\'\\s\*clns\\s\+routing\'\,\"

你自己试过什么吗?请参阅常见问题解答:我尝试了这个,re.match^self\。不支持的\u cmds\?=\?[r\'.*\,line您应该在问题中记录这个问题,并描述您的代码遇到的特定问题错误消息、异常等。匹配的条件是什么?因为您没有声明wim的答案是有效的。