Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.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_Metacharacters - Fatal编程技术网

Python正则表达式和元字符

Python正则表达式和元字符,python,regex,metacharacters,Python,Regex,Metacharacters,有一个变量是: line="s(a)='asd'" 我试图找到一个包含“s()”的零件 我尝试使用: re.match("s(*)",line) 但它似乎无法搜索包含() 有没有办法找到它并用python打印出来?这里的问题是正则表达式 您可以使用: >>> line="s(a)='asd'" >>> print re.findall(r's\([^)]*\)', line) ['s(a)'] 正则表达式分解: s # match letter

有一个变量是:

line="s(a)='asd'"
我试图找到一个包含“s()”的零件

我尝试使用:

re.match("s(*)",line)
但它似乎无法搜索包含()


有没有办法找到它并用python打印出来?

这里的问题是正则表达式

您可以使用:

>>> line="s(a)='asd'"
>>> print re.findall(r's\([^)]*\)', line)
['s(a)']
正则表达式分解:

s     # match letter s
\(    # match literal (
[^)]* # Using a negated character class, match 0 more of any char that is not )
\)    $ match literal (
  • r
    用于Python中的原始字符串

我建议您熟悉正则表达式语法:(例如,
*
不会做您可能认为会做的事情)。为什么他会被否决?谢谢您的快速回复!您可能还应该提到
r'…'
字符串的好处。(虽然在本例中,这并没有什么区别。)