开发者

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?


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

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


Try

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

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


Edit:

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

Also works.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜