我有几行文本,例如下面两行:
a[15].s16.l = (xy[11].s16.l > (50/*QUE-const:VECTWord->CC_Init_P*/))
xyz = Exh[(16/*QUE-const:VECT_dir->_num_P*/) & 0x0FU ];*/))
我想匹配具有"0"的行;QUE常量";并以"_P〃;方括号[]内。
我写了以下正则表达式:
[.*QUE-const.*_P.*
但它同时匹配两行,而应该只匹配第二行。
请检查并纠正我哪里出了问题。
对于您显示的示例,您可以尝试以下操作吗。
[.*?QUE-const.*?_P.*?]
这里是上面regex 的在线演示
解释:添加以上详细解释。
[.*?QUE-const.*?_P.*?]
##Matching [ and till QUE-const then match till _P(with non-greedy quantifier) till first occurrence of ] here.
我相信你很接近。以下是我的看法:
^.*[.*QUE-const.*_P.*].*$
Regex Demo
说明:
^ # start of line
.* # match anything 0 to unlimited times
[ # match bracket 1
.*QUE-const.* # match string containing QUE-const ...
_P.* # ends on _P and !!! anything after (in your example that should match you have */ after _P )
] # match bracket 2
.* # match anything after 0 to unlimited times
$ # end of line
您还可以使用以[^][]*
开头的否定字符类,以在匹配文本时不通过方括号边界。
[[^][]*QUE-const[^][]*_P[^][]*]
Regex演示
或者,如果你想匹配整条线:
^.*?[[^][]*QUE-const[^][]*_P[^][]*].*$
模式匹配:
^
字符串开始.*?
尽可能少地匹配任何字符[[^][]*
匹配开头的[
,然后匹配除[
和]
之外的0+倍字符QUE-const
按字面匹配[^][]*
匹配除[
和]
之外的任何字符0+次_P
按字面匹配[^][]*]
匹配除[
和]
之外的任何字符的0+倍,然后匹配结束的]
.*
匹配0+倍任意字符$
字符串末尾
Regex演示