stillmotion
stillmotion

Reputation: 4628

Greedy Regex Matching

I'm trying to match a string that looks something like this:

<$Fexample text in here>>

with this expression:

<\$F(.+?)>{2}

However, there are some cases where my backreferenced content includes a ">", thus something like this:

<$Fexample text in here <em>>>

only matches example text in here <em in the backreference. What do I need to do to conditionally return a correct backrefernce with or without these html entities?

Upvotes: 2

Views: 1159

Answers (3)

Michael
Michael

Reputation: 355

Please note than tu truly do what (I think) you want to do, you would have to interpret well-formed bracket expressions, which is not possible in a regular language.

In other words, <$Fexample <tag <tag <tag>>> example>> oh this should not happen> will return example <tag <tag <tag>>> example>> oh this should not happen as the capture group.

Upvotes: 0

codaddict
codaddict

Reputation: 455342

You can add start and end anchors to the regex as:

^<\$F(.+?)>{2}$

Upvotes: 5

kennytm
kennytm

Reputation: 523584

Try

<\$F(.+?)>>(?!>)

The (?!>) forces only the last >> in a long sequence of >>>..>>> will be matched.


Edit:

<\$F(.+?>*)>>

Also works.

Upvotes: 3

Related Questions