如何修改这个正则表达式以匹配所有三种情况?

2024-04-25 13:49:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我想测试一个方法是否出现在头文件中。我有三个案子:

void aMethod(params ...)
//void aMethod(params
// void aMethod(params
  ^ can have any number of spaces here

以下是我目前掌握的情况:

re.search("(?<!\/\/)\s*void aMethod",buffer)

但是这只会匹配第一种情况,第二种情况。我怎么能把它调整到和第三个匹配呢?你知道吗

你知道吗编辑:对不起,但我没有正确地表达自己。我只想匹配,如果不是在一个评论。非常抱歉。你知道吗


Tags: of方法renumberhere头文件have情况
3条回答

编辑:好的,编辑之后,Geo,是这样的:

^(?<!\/\/)\s*void aMethod

如果您只想查找“void aMethod(params)”的所有外观,则可以使用以下选项:

a = """void aMethod(params ...)
//void aMethod(params
// void aMethod(params
  ^ can have any number of spaces here"""
from re import findall
findall(r'\bvoid aMethod\b', a)

或:

findall(r'(?:\/\/)?[ ]*void aMethod', a)

试试这个regexp。你知道吗

(\/\/)?\s*void aMethod

相关问题 更多 >