Regular Expression for paired brackets
Asked Answered
F

2

2

The Input Line goes like this (just a part of it):

[[Text Text Text]][[text text text]]asdasdasdasda[[asdasdasdasd]]

What I want is to list all matches wherein text enclosed in a pair of [[ and ]]. I did try several patterns, but all fails when a unclosed [[ or ]] is within the input line. For example:

[[text[[text text TEXT text]]

Also, what if a single bracket exist within the input line, like:

[[text [text] text TEXT text]]

The regex pattern I used was:

\[\[[^\[\[]*\]\]
Farra answered 6/3, 2012 at 8:39 Comment(0)
S
4
\[\[(?:(?!\[\[|\]\]).)*\]\]

matches [[<anything>]], where <anything> may not contain double brackets but everything else. You might need to set the DOTALL option of your regex engine to allow the dot to match newlines.

It will match [[match [this] text]] in

[[don't match this [[match [this] text]] don't match this]]

Explanation:

\[\[     # Match [[
(?:      # Match...
 (?!     #  (unless we're right at the start of
  \[\[   #   [[
 |       #  or
  \]\]   #   ]]
 )       #  )
 .       # ...any character
)*       # Repeat any number of times
\]\]     # Match ]]

Caveat: It will match [[match [this]] in the text [[match[this]]] because regexes can't count.

Steato answered 6/3, 2012 at 9:31 Comment(0)
J
2

What to do with unclosed or single brackets is up to your specification. Until then

\[\[([^\]]|\][^\]])*\]\]

works for me (where the whole of the two problematic strings are matched.

Jamshedpur answered 6/3, 2012 at 8:48 Comment(3)
@sweaver2112 You mean for a generic regex across the various implementations? The above was tested with .NET.Jamshedpur
would there be any where escaping it would hurt? your pattern works, but on regexr it did not until i escaped the ]. it is, after all, a special char and the regex engine may not be smart enough to figure it out.Molybdenite
Yeah, I missed the backslash for the ] after the | and I've added them for the classes too.Jamshedpur

© 2022 - 2024 — McMap. All rights reserved.