简体   繁体   English

在TCL中匹配regexp的问题

[英]Issue in matching regexp in TCL

I am having following pattern 我有以下模式

Notif[0]:
some text multiple line
Notif[1]:
multiple line text
Notif[2]:
text again
Notif[3]:
text again
Finish

I am writting following regexp 我正在撰写以下正则表达式

set notifList [regexp -inline -all -nocase {Notif\[\d+\].*?(?=Notif|Finish)} $var]

It is not giving desired output 它没有提供所需的输出

Output needed 需要输出

I need a list with each `Notif`block

The reason is that your .*? 原因是你的.*? acts as a greedy subpattern (= .* matching 0+ any characters incl. a newline) because the first quantifier in the pattern was a greedy one (see \\d+ ). 充当贪婪的子模式(= .*匹配0+任何字符,包括换行符),因为模式中的第一个量词是贪婪的(参见\\d+ )。 See this Tcl Regex reference : 请参阅Tcl Regex参考

A branch has the same preference as the first quantified atom in it which has a preference. 分支与其中具有偏好的第一个量化原子具有相同的偏好。

You need to just turn the first + quantified subpattern into a lazy one by adding a ? 你需要通过添加一个?将第一个+量化的子模式转换为惰性模式? after it: 之后:

Notif\[\d+?\].*?(?=Notif|Finish)
          ^

This will prevent the .*? 这会阻止.*? pattern to inherit the greediness from the \\d+ . 模式继承了\\d+的贪婪。

See the IDEONE demo 请参阅IDEONE演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM