Question

I have a string and I want to match the content of double square brackets: Example:

<p><span>"Sed ut perspiciatis vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"</span></p><p><span>[[image file="2013-12/5s_1.jpg" alt="IPhone 5s" title="IPhone 5s" ]]</span></p><p><span>[[download file="2013-12/modulo-per-restituzione-prodotti-.pdf" icon="icon" text="" title="Download" ]]</span></p>

Results:

download file="2013-12/module-res.pdf" icon="icon" text="" title="Download" 

image file="2013-12/5s_1.jpg" alt="IPhone 5s" title="IPhone 5s" 

Consider that these 2 strings can contain any type of characters, I tried this solution but I have problems with other characters:

\[\[[\w+\s*="-\/]*\]\]
Was it helpful?

Solution

What about using a negated character class

\[\[[^\]]*\]\]

This class would match anything but "]"

See it here on Regexr

To avoid the square brackets beeing part of the result, you can either use a capturing group

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

and get the result from group 1

or use lookaround assertions (if it is supported by your regex engine)

(?<=\[\[)[^\]]*(?=\]\])

See it on Regexr

OTHER TIPS

If you can use lookaheads:

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

meaning:

\[\[    # match 2 literal square brackets
 (      # match
    [^]]         # a non-square bracket
    |            # or
    []](?!\])    # a square bracket not followed by a square bracket
 )*     # any number of times
\]\]    # match 2 literal right square brackets

Or you can use lazy quantifiers:

\[\[(.*?)\]\]

This regexp will select the square brackets but by using group(1) you will be able to get only the content:

"\\[\\[\\(.*\\)\\]\\]"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top