Ivan Zlatanov
Ivan Zlatanov

Reputation: 5226

Regex to match content between given characters

What is need is given the following string "Other Text [[MyText]] Some Other Text", and given some specific characters like "[[" for start and "]]" for end, to be able to match "[[MyText]]".

Given the following regex = "(\[\[)(.*)(\]\])" I am able to match what I want, but not exactly.

Lets say I have this string "Other Text[[ Something...[[MyText]]......something...[[MyOtherText]]" - I need to have 2 matches here "[[MyText]]" && "[[MyOtherText]]". My regex is too eager and matches more than I really want to.

Please, besides the answer I am looking to understand how it works, so a few comments as well will be helpful. Thanks.

Upvotes: 0

Views: 1205

Answers (3)

C. Ross
C. Ross

Reputation: 31848

What you need is this :

regex = "\[\[([^\[]*)\]\]"

It will will blow up if you have a '[' in your text your trying to match, or nested pairs.

The reason it works this way is the default greedy matching of most regex implementations. In short greedy matching is taking as much as the match could possibly cover. The reason my regex works is that it does not allow another "[" (so it can't greedy match into the next set). Enjoy your wiki work.

Upvotes: 1

Rubens Farias
Rubens Farias

Reputation: 57936

Maybe you're looking for non-greedy pattern or 'perlre'.

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

Note that additional '?' character;

Upvotes: 6

Bart Kiers
Bart Kiers

Reputation: 170138

Try:

\[{2}(?:(?!]]).)*]]

Also works with single ['s and ]'s inside your double [[ and ]]'s.

For a good explanation, see: http://www.regular-expressions.info/repeat.html

Note that ] is not a meta character (outside a character class), so there's no real need to escape it.

Upvotes: 0

Related Questions