Regex 匹配具有特定文本且以方括号内的_P结尾的行
我有几行文字,例如,下面有两行:Regex 匹配具有特定文本且以方括号内的_P结尾的行,regex,Regex,我有几行文字,例如,下面有两行: 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 ];*/)) 我想匹配具有“QUE const”且以方括号[]内的“_p”结尾的行 我写了以下正则表达式: \[.*QUE-const.*_P.* 但它同时匹配两条线,而应该只匹配第二条线
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 ];*/))
我想匹配具有“QUE const”且以方括号[]内的“_p”结尾的行
我写了以下正则表达式:
\[.*QUE-const.*_P.*
但它同时匹配两条线,而应该只匹配第二条线
请检查并纠正我的错误。对于您展示的样品,请尝试以下内容
\[.*?QUE-const.*?_P.*?\]
说明:添加上述内容的详细说明
\[.*?QUE-const.*?_P.*?\]
##Matching [ and till QUE-const then match till _P(with non-greedy quantifier) till first occurrence of ] here.
我相信你很接近。以下是我对它的看法:
^.*\[.*QUE-const.*_P.*\].*$
说明:
^ # 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[^][]*]
或者,如果要匹配整行:
^.*?\[[^][]*QUE-const[^][]*_P[^][]*].*$
模式匹配:
字符串的开头^
尽可能少地匹配任何字符*?
匹配开头\[[^][]*
然后匹配0+次字符,除了[
和[
]
按字面匹配QUE const
匹配除[^][]*
和[
]
逐字匹配\u P
匹配除[^][]*]
和[
之外的任何字符的0+倍,然后匹配结束符]
]
匹配任何字符的0+倍*
字符串结尾$
有很多方法可以满足您的需求,主要有两种:1)匹配左侧上下文(),2)仅在方括号内匹配(),您的第二个演示似乎效果最好!谢谢!我看我的想法已经被采纳了。