Click here to Skip to main content
15,069,297 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I'm kind of stuck on this one

I need to filter out "all" levels of square brackets with a leading character
= X[100*[5]] out of a string
After locking around found this :
(?<=X) (?\[(?>[^\[\]]+| \[ |\])*(?(number)(?!))\])

This works pretty good and seems to catch any level of brackets but now I discovered a Problem :-(
If I have two "matching sets" it will include them in a single match ..

Therefore X[100*[5]]Y[5/2]

will result in the Reg catching the whole line, but what I need , is to catch only the portion with the leading X (and not to include the second set with the leading Y)

Any ideas?
Updated 21-Nov-11 18:31pm

Look in your Regex documentation for "non-greedy". By default, matching operators are "greedy", that is they match as much as possible. What you need is non-greedy, which matches as little as possible. For example, looking at string 1234abc, the regex fragment \d+ matches 1234 but the non-greedy \d+? only matches 1. Use a tool like Expresso to build and test your regex.

But isn't the greedy what makes this "work"? = capture as many [Bracket]levels as possible ??

Anyway- I'll look into it - maybe I just need to find another approach to capture the [brackets]

How can this be an answer?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900