开发者

Problem using Regex.Replace when text containing $ and using \b boundry

开发者 https://www.devze.com 2023-02-06 11:36 出处:网络
have the following problem using regex and $ in a string. Wondring if anyone can assist. bla in text below is random words.

have the following problem using regex and $ in a string. Wondring if anyone can assist.

bla in text below is random words.

string text = "<id='$text1$text2$text3'><div>bla bla bla text3 bla bla</div>";
string pattern = "\btext3\b";
text = Regex.Replace(text, pattern, "####");

If i do above, it will replace both text3. I only want to change the value in the div element so the result becomes: <id='$text1$text2$text3'> <div>bla bla bl开发者_Go百科a #### bla bla</div>.

Thanks in advance!


string pattern = @"\btext3\b(?![^<>]*>)";

This quick-and-dirty solution relies on several simplifying assumptions, as all regexes must if they're to be used on HTML. For example, it assumes there will never be any angle brackets in attribute values. That's legal (in HTML at least), but it's extremely rare in practice.


string pattern = ">text3<";
text = Regex.Rplace(text,pattern,">####<");
0

精彩评论

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

关注公众号