匹配 PCRE 中没有最外括号的括号内容

发布于 2024-07-30 12:46:40 字数 408 浏览 5 评论 0原文

我需要提取不平衡括号结构的内容。 在 PCRE 手册中,我找到了匹配平衡括号的解决方案。

<\[ ( (?>[^(<\[|\]>)]+) | (?R) )* \]>

对于我的测试,

<[<[ab<[cd]>]><[ef]>

它提取

0.0: <[ab<[cd]>]>
0.1: <[ef]>

但我想提取相同的内容而不需要最外层的括号:

0.0: ab<[cd]>
0.1: ef

有人能指出解决方案吗?

I need to extract content of unbalanced paren construction. In manual for PCRE i found solution for matching balanced parens.

<\[ ( (?>[^(<\[|\]>)]+) | (?R) )* \]>

For my test

<[<[ab<[cd]>]><[ef]>

It extracts

0.0: <[ab<[cd]>]>
0.1: <[ef]>

But i want to extract same content without outermost parens:

0.0: ab<[cd]>
0.1: ef

Could anybody point a solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ま昔日黯然 2024-08-06 12:46:40

好吧,从正则表达式的外观来看,最外层外壳内的内容(在该术语的任何正常用法中它们都不是括号)已经被捕获在括号组中。 我不知道您在什么上下文中使用 PCRE 库,但您想要的提取应该出现在“match #1”中(其中整个模式匹配是 match #0)。 即您的数据应该已经如下所示:

0.0: <[ab<[cd]>]>
0.1: <[ef]>
1.0: ab<[cd]>
1.1: ef

Well, from the look of your regex, the content inside the outermost enclosure (they're not parentheses in any normal usage of the term) is already being captured in a parenthesis group. I don't know what context you're using the PCRE library in, but the extractions you want should be present in "match #1" (where the entire pattern match is match #0). i.e. your data should already look like:

0.0: <[ab<[cd]>]>
0.1: <[ef]>
1.0: ab<[cd]>
1.1: ef
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文