正则表达式只匹配最里面的分隔序列

amp*_*ine 3 regex perl delimiter lookahead

我有一个字符串,其中包含由多个字符分隔的序列:<<>>.我需要一个正则表达式才能给出最里面的序列.我已经尝试过前瞻,但它们似乎没有像我期望的那样工作.

这是一个测试字符串:

'do not match this <<but match this>> not this <<BUT NOT THIS <<this too>> IT HAS CHILDREN>> <<and <also> this>>'
Run Code Online (Sandbox Code Playgroud)

它应该返回:

but match this
this too
and <also> this
Run Code Online (Sandbox Code Playgroud)

正如你可以看到的第三个结果,我不能只使用/<<[^>]+>>/因为字符串可能有一个分隔符,但不是连续两个.

我刚从试错中恢复过来.在我看来,这不应该是这么复杂.

ike*_*ami 8

@matches = $string =~ /(<<(?:(?!<<|>>).)*>>)/g;
Run Code Online (Sandbox Code Playgroud)

(?:(?!PAT).)*就是[^CHAR]*人物的模式.


yst*_*sth 6

$string = 'do not match this <<but match this>> not this <<BUT NOT THIS <<this too>> IT HAS CHILDREN>> <<and <also> this>>';
@matches = $string =~ /(<<(?:[^<>]+|<(?!<)|>(?!>))*>>)/g;
Run Code Online (Sandbox Code Playgroud)