我有下一个正则表达式:
comment_pattern = "(/\*[\w\W]*\*/)"
有了它,我能够搜索匹配字符串如below:
/*blablabla example blabla*/
/*
blablabla example
blabla
*/
基本上我也想在这些注解中搜索变量Compiler_Warning -〉以防它在多行注解中得到所有表达式-〉有人能告诉我如何得到它吗?基本上我的正则表达式应该返回一个匹配:
/* blablaCompiler_Warning blablalba*/
/* blabla
Compiler_Warning blablalba
但不是第一个例子。
8yparm6h1#
尝试(regex demo):
import retext = """\/*blablabla example blabla*/Not comment/* blablaCompiler_Warning blablalba*/"""pat = re.compile(r"/\*(?=(?:(?!\*/).)*?Compiler_Warning).*?\*/", flags=re.S)for comment in pat.findall(text): print(comment)
import re
text = """\
Not comment
*/"""
pat = re.compile(r"/\*(?=(?:(?!\*/).)*?Compiler_Warning).*?\*/", flags=re.S)
for comment in pat.findall(text):
print(comment)
印刷品:
0pizxfdo2#
如果不想在示例的开头和结尾之间交叉匹配/*和*/:
(?s)/\*(?:(?!\*/|/\*).)*?\bCompiler_Warning\b(?:(?!\*/|/\*).)*\*/
说明
(?s)
/\*
(?:(?!\*/|/\*).)*?
\bCompiler_Warning\b
(?:(?!\*/|/\*).)*
\*/
请参阅regex demo和Python demo
2条答案
按热度按时间8yparm6h1#
尝试(regex demo):
印刷品:
0pizxfdo2#
如果不想在示例的开头和结尾之间交叉匹配
/*
和*/
:说明
(?s)
内联修饰符,使点也与换行符匹配/\*
匹配/*
(?:(?!\*/|/\*).)*?
匹配后面没有直接跟*/
或/*
的任何字符\bCompiler_Warning\b
在单词边界之间逐字匹配(?:(?!\*/|/\*).)*
匹配后面没有直接跟*/
或/*
的任何字符\*/
匹配*/
请参阅regex demo和Python demo