[英]Tcl regexp : Why '+' does not match as many as possible?
I am using TCL8.4. 我正在使用TCL8.4。 In the following expression, I tried to fetch the numerical value using ([0-9]+). 在下面的表达式中,我尝试使用([0-9] +)获取数值。 But it does not matches as many as possible though man page shows '+' is meant for matching as many as possible ( ref : http://wiki.tcl.tk/396 ) Also, please share/suggest any better way of doing what I want to do. 但它尽可能不匹配尽管手册页显示'+'意味着尽可能多地匹配(参考: http : //wiki.tcl.tk/396 )另外,请分享/建议任何更好的做法我想做的事。
%set a {
NOTPLD STATS:
Bps: 0; pps: 0; Bytes: 0; Packets: 4535
TPLD STATS:
Bps: 0; pps: 0; Bytes: 0; Packets: 4535
}
%
% regexp {NOTPLD STATS:(.*?)Packets:[\s]+([0-9]+)} $a t1 t2 c
1
% set c
4
See Interaction Between Quantifiers with Different Greediness : 查看具有不同贪婪的量词之间的相互作用 :
All quantifiers in a branch get switched to the same greediness, so adding a non-greedy quantifier makes the other quantifiers in the branch implicitly non-greedy as well. 分支中的所有量词都会切换到相同的贪心,因此添加非贪婪的量词会使分支中的其他量词也隐含非贪婪。
Thus, your ([0-9]+)
is interpreted as ([0-9]+?)
, and it matches one or more digits, but as few as possible to return a valid match. 因此,您的([0-9]+)
被解释为([0-9]+?)
,它匹配一个或多个数字,但尽可能少地返回有效匹配。 All lazy subpatterns at the end of patterns only match zero ( *?
) or one ( +?
) symbols. 模式结尾处的所有惰性子模式仅匹配零( *?
)或一( +?
)符号。
A simple solution is just to add a trailing character, here, it is a newline (or whitespace): 一个简单的解决方案就是添加一个尾随字符,这里是一个换行符(或空格):
regexp {NOTPLD STATS:(.*?)Packets:[\s]+([0-9]+)\s} $a t1 t2 c
^
See IDEONE demo 请参阅IDEONE演示
If the value can be at the end of the string, use an alternation (?:\\s|$)
. 如果值可以在字符串的末尾,请使用替换(?:\\s|$)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.