3

I have a very simple regex that seems to work in in online regex testing systems but when I plug it into my code it fails.

I would like to remove items between two comments in html.

I am using the following code to do so:

string source = x;
String matchpattern = @"<!-- link -->.*<!-- /link -->";
String replacementpattern = @"";
string result = Regex.Replace(source, matchpattern, replacementpattern);

where x is the following:

<!-- link -->\n\t\t\t\t\t\t<p class=\"backToTop\"><a href=\"#content\">    Back to top</a></p>\n\t\t\t\t\t\t<!-- /link -->

If anyone has any ideas then they would be much appreciated.

0

2 Answers 2

3

The \n in x is confusing it... but it's easy to fix. Just add a fourth argument of RegexOptions.Singleline so that . still matches \n.

to your Replace call:

string result = Regex.Replace(source, matchPattern, replacementPattern,
                              RegexOptions.Singleline);
Sign up to request clarification or add additional context in comments.

1 Comment

@Prib: Just be aware that things will go bad if x contains more than one pair of comments (because the regex will match from the first <!-- link --> to the last <!-- /link -->). Using .*? instead of .* makes this a little safer.
0

Set single line option (?s), e.g.: (?s)<!-- link -->.*<!-- /link -->

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.