Reputation: 161
I have a Regex as follows:
Pattern: <navPoint id="navPoi[^"]+" playOrder="[^"]+"><navLabel><text>([^<>\r\n]+)</text></navLabel><content src="([^<>\r\n]+)"/>$
Substitution: <li id="NavPoint-#"><a href="$2">$1</a>\r\n<ol>
It works in normal editors and even in regex101: Demo with Input
I want to write the same in C#, but when I do, it does not work. Am I doing something wrong?
Here is my code:
string secondPattern = @"<navPoint id=""navPoi[^""]+"" playOrder=""[^""]+""><navLabel><text>([^<>\n]+)<\/text><\/navLabel><content src=""([^<>\n]+)""\/>$";
string secondSubstitution = @"<li id=""NavPoint-#""><a href=""$2"">$1</a>\r\n<ol>";
Regex secondRegex = new Regex(secondPattern, options);
string anotherNavMap = secondRegex.Replace(newNavMap, secondSubstitution);
Upvotes: 2
Views: 115
Reputation: 626758
You should make sure you pass RegexOptions.Multiline
option to the regex compile, and allow the CR before the $
end of line anchor as $
in multiline mode only matches before an LF symbol.
Also, @"\r\n"
is a string of 4 characters, if you need a CRLF linebreak, use "\r\n"
, as verbatim string literals do not support string escape sequences.
Here is a code fix:
string secondPattern = @"<navPoint id=""navPoi[^""]+"" playOrder=""[^""]+""><navLabel><text>([^<>\n]+)</text></navLabel><content src=""([^<>\n]+)""/>\r?$";
string secondSubstitution = "<li id=\"NavPoint-#\"><a href=\"$2\">$1</a>\r\n<ol>";
Regex secondRegex = new Regex(secondPattern, RegexOptions.Multiline);
string anotherNavMap = secondRegex.Replace(newNavMap, secondSubstitution);
Upvotes: 1