Using the following test string (note, I added an additional [[image:foobar[[foo [baz] bar]]foobar]]
in there):
[[Image:ChicagoAnarchists.jpg|thumb|A sympathetic engraving by [[Walter Crane]] of the executed \"Anarchists of Chicago\" after the [[Haymarket affair]]. The Haymarket affair is generally considered the most significant event for the origin of international [[May Day]] observances]] In 1907, the [[International Anarchist Congress of[[image:foobar[[foo [baz] bar]]foobar]] Amsterdam]] gathered delegates from 14 different countries, among which important figures of the anarchist movement, including [[Errico Malatesta]]
And a regular expression pattern of:
(?i)\\[\\[image:(?:\\[\\[(?:(?!(?:\\[\\[|]])).)*]]|(?:(?!(?:\\[\\[|]])).)*?)*?]]
testString.replaceAll(<above pattern>, "")
will return:
In 1907, the [[International Anarchist Congress of Amsterdam]] gathered delegates from 14 different countries, among which important figures of the anarchist movement, including [[Errico Malatesta]]
Here's a more detailed explanation of the regular expression:
(?i) # Case insensitive flag
\[\[image: # Match literal characters '[[image:'
(?: # Begin non-capturing group
\[\[ # Match literal characters '[['
(?: # Begin non-capturing group
(?! # Begin non-capturing negative look-ahead group
(?: # Begin non-capturing group
\[\[ # Match literal characters '[['
| # Match previous atom or next atom
]] # Match literal characters ']]'
) # End non-capturing group
) # End non-capturing negative look-ahead group
. # Match any character
) # End non-capturing group
* # Match previous atom zero or more times
]] # Match literal characters ']]'
| # Match previous atom or next atom
(?: # Begin non-capturing group
(?! # Begin non-capturing negative look-ahead group
(?: # Begin non-capturing group
\[\[ # Match literal characters '[['
| # Match previous atom or next atom
]] # Match literal characters ']]'
) # End non-capturing group
) # End non-capturing negative look-ahead group
. # Match any character
) # End non-capturing group
*? # Reluctantly match previous atom zero or more times
) # End non-capturing group
*? # Reluctantly match previous atom zero or more times
]] # Match literal characters ']]'
This will only handle one level of nested [[...]]
patterns. As noted in this answer to this question that TJR commented about above, regular expressions will not handle unlimited nested atoms. So this regular expression pattern will not match something like [[foo[[baz]]bar]]
within a [[image:...]]
string.
For a great regular expressions reference, see Regular-Expressions.info.