正则表达式仅匹配最里面的分隔序列
我有一个字符串,其中包含由多个字符分隔的序列:<<
和 >>
。我需要一个正则表达式来只给出最里面序列。我已经尝试过前瞻,但它们似乎没有按照我期望的方式工作。
这是一个测试字符串:
'do not match this <<but match this>> not this <<BUT NOT THIS <<this too>> IT HAS CHILDREN>> <<and <also> this>>'
它应该返回:
but match this
this too
and <also> this
正如您在第三个结果中看到的,我不能只使用 /<<[^>]+>>/
因为字符串可以有一个分隔符字符,但不能连续有两个字符。
我刚从反复试验中走出来。在我看来这不应该这么复杂。
I have a string that contains sequences delimited by multiple characters: <<
and >>
. I need a regular expression to only give me the innermost sequences. I have tried lookaheads but they don't seem to work in the way I expect them to.
Here is a test string:
'do not match this <<but match this>> not this <<BUT NOT THIS <<this too>> IT HAS CHILDREN>> <<and <also> this>>'
It should return:
but match this
this too
and <also> this
As you can see with the third result, I can't just use /<<[^>]+>>/
because the string may have one character of the delimiters, but not two in a row.
I'm fresh out of trial-and-error. Seems to me this shouldn't be this complicated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(?:(?!PAT).)*
之于模式,正如[^CHAR]*
之于字符。(?:(?!PAT).)*
is to patterns as[^CHAR]*
is to characters.这是使用
split
完成任务的方法:将标签保留在那里,如果您希望将它们删除,只需执行以下操作:
Here's a way to use
split
for the job:Keeps the tags in there, if you want them removed, just do: