Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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正则表达式进行C预处理_Python_Regex - Fatal编程技术网

用Python正则表达式进行C预处理

用Python正则表达式进行C预处理,python,regex,Python,Regex,我以前从未使用过正则表达式,我正在努力理解它们。我有'define(\uuuu arch64\uuuuu)形式的字符串,我只想要\uuuuuu arch64\uuuu import re mystring = 'define(this_symbol)||define(that_symbol)' pattern = 'define\(([a-zA-Z_]\w*)\)' re.search(mystring, pattern).groups() (没有,没有) 什么不search返

我以前从未使用过正则表达式,我正在努力理解它们。我有
'define(\uuuu arch64\uuuuu)
形式的字符串,我只想要
\uuuuuu arch64\uuuu

import re  
mystring = 'define(this_symbol)||define(that_symbol)'  
pattern = 'define\(([a-zA-Z_]\w*)\)'  
re.search(mystring, pattern).groups()  
(没有,没有)


什么不
search
返回
'this\u symbol'
'that\u symbol'

您必须区分符号
和regexp组字符。此外,模式位于:


您的参数顺序错误,应为:

re.search(pattern, mystring)
此外,反斜杠是python字符串中的转义字符(例如“\n”将是包含换行符的字符串)。如果要使用文字反斜杠,如在正则表达式中,则必须使用另一个反斜杠对其进行转义。或者,您可以使用前面有
r
标记的反斜杠,而不要将反斜杠视为转义字符:

pattern = r'define\(([a-zA-Z_]\w*)\)'

这就解决了问题。添加r似乎没有什么不同。@Neil:可能是因为
\(
\)
\w
在Python字符串中不是有效的转义序列,因此它们不会被其他任何东西所取代。但通常我建议在处理应该包含反斜杠的字符串时使用
r
pattern = r'define\(([a-zA-Z_]\w*)\)'