符合括号内的所有内容的正则忽略嵌套
我正在寻找一个正则表达式规则来匹配方括号内的所有内容,包括括号并忽略内部可能的括号。例如:
[value] in the [text[42]] and [1[2[3]]]!
我需要提取 [value]
、[text[42]]
和 [1[2[3]]]
。
我使用
\[(.*?)\]
这里有更多示例 - https://regex101.com/r/Xp4ghi/1
PS我将在.NET中使用它
I am looking for a regex rule to match everything inside square brackets, including the brackets and ignoring the possible brackets inside. E.g. from:
[value] in the [text[42]] and [1[2[3]]]!
I need to extract [value]
, [text[42]]
and [1[2[3]]]
.
I use
\[(.*?)\]
Here are more expamples - https://regex101.com/r/Xp4ghi/1
P.S. I am going to use it in .NET
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
regex demo 。 详细信息:
\ [
- a[
char(?> [^] [] []+|(? \ [|(?< -c>)])+
- 一次或多个出现[^] []+|
- 一个或多个字符以外的]
和[
,或,或(?
(?
]
- a]
char。You can use
See the regex demo. Details:
\[
- a[
char(?>[^][]+|(?<c>)\[|(?<-c>)])+
- one or more occurrences of[^][]+|
- one or more chars other than]
and[
, or(?<c>)\[|
- a[
char and a value is pushed onto Group "c" stack, or(?<-c>)]
- a]
char and a value is popped off the Group "c" stack]
- a]
char.