tags:

views:

657

answers:

3

I have the following regex, where I want to match any explicit dot followed by one or more:

<b> <i> <u> </b> </i> </u>

I would like this Regex to NOT match this pattern if it occurs at the end of the string.

string = Regex.Replace(string, "\.((<[\/biu]+>)+)", ".$1||")

Ex:

This <b>should match.</b> allright.

This <i><b>shouldn't match.</b></i>
+6  A: 
"\.((<[\/biu]+>)+)(?!$)"

Use the negative lookahead assertion with the $ symbol to check for end of line. (Remember, $ matches end of line so you want to not match that.)

Evan Fosmark
Thanks, but it still matches ".</b>" in the following:This <i><b>shouldn't match.</b></i>
Vincent
You could always make it non-greedy by introducing the `?` symbol. That would probably make it not match what you wrote. (I don't have any resource to test with right now)
Evan Fosmark
+2  A: 

Force there to be more items after the last closed element, but make sure they aren't elements themselves.

"\.((<[\/biu]+>)+)[^<>]+"
David Morton
+4  A: 

You could use atomic grouping:

\.(?>(?:<\/?[biu]>)+)(?!$)
Gumbo
The question was to match any *dot*: "\.(?=(?>(?:<\/?[biu]>)+)(?!$))". :-) Otherwise, +1
Tomalak
The OP was capturing the tags and plugging them back in with $1, so he should add capturing parens instead of a lookahead. Also, this is the only answer that corrects the OP's mistake WRT matching the tags.
Alan Moore