0

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*="-\/]*\]\]
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
The_Guy
  • 215
  • 1
  • 2
  • 7

3 Answers3

1

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

stema
  • 90,351
  • 20
  • 107
  • 135
  • Ok, this is perfect for my scope. Thank you @stema your solution without the brackets is very useful. – The_Guy Dec 16 '13 at 10:58
1

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:

\[\[(.*?)\]\]
perreal
  • 94,503
  • 21
  • 155
  • 181
0

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

"\\[\\[\\(.*\\)\\]\\]"
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115