在 TCL 中匹配正则表达式的问题

Issue in matching regexp in TCL

我有以下模式

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

我正在写正则表达式

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

它没有提供所需的输出

需要输出

I need a list with each `Notif`block

原因是您的 .*? 充当贪婪子模式(=.* 匹配 0+ 任何字符,包括换行符)因为模式中的第一个量词是贪婪的(请参阅\d+)。见 this Tcl Regex reference:

A branch has the same preference as the first quantified atom in it which has a preference.

您只需将第一个 + 量化子模式转换为惰性子模式,方法是在其后添加 ?:

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

这将防止 .*? 模式继承 \d+ 的贪婪模式。

IDEONE demo