Forum Moderators: coopster

Message Too Old, No Replies

regex question

         

Mister_L

3:37 pm on Feb 5, 2012 (gmt 0)

10+ Year Member



I need to match a word or expression,on the condition it is not inside curly braces in any way. For example, I need to match only the first word "TARGET" in the following (arbitrary) string:

text text TARGET {text TARGET text text} text text.

The order of appearance may vary, and the string may not even contain curly braces at all. The word TARGET may appear several times outside of curly braces, but I only need the first occurrence.

How do I accomplish this?

Thanks in advance.

g1smd

3:41 pm on Feb 5, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In two steps, remove everything inside curly braces and the curly braces, then search the remainder for the target word.

lucy24

11:00 pm on Feb 5, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That was Option A. Option B is something I've done a lot in text editing. Assuming you don't have nested braces, and there's no chance of meeting a closing brace before an opening brace, it runs something like this:

^(([^{]*?{[^}]*})*?[^{]*?)TARGET

The *? combination is not a happy one, but you need it here. The ? does not mean that the preceding part is optional. Coming after * or + it means "stop as soon as you can" instead of the default "go on as long as you can".

The pieces are:

^ = start at the very beginning, so you can't cheat by grabbing something from the middle

[^{]*? = the text may or may not contain stuff before the target word AND before the first brace.

{[^}]*} = at some point there may be an open-brace, possibly containing stuff, followed by a close-brace. Here it doesn't matter if you say * or *? because either way you have to continue until you meet a close-brace, and you can't go on any further.

([^{]*?{[^}]*})*? = this whole package might occur more than once.

[^{]*? = there may or may not be more stuff between the closing bracket-- assuming there was one-- and your target word

(([^{]*?{[^}]*})*?[^{]*?) = capture everything you've got so far. If you don't need to capture the part before the target word, you can leave off the outer set of parentheses.