简体   繁体   中英

Match escaped html in regex c#

How can I escape html codes in Regex?

I need to find the string

&

in a string like

this is my string & this is another string

I can not use HtmlEncode/Decode for this purpose cause i need work with tags. That i want i just find the common string.

I use this, and work for example with "another" or "my" but doesn't work with "&" .

            Regex regularextest = new Regex("\b&\b", options);
            string RSTest = "char $& morechar";
            string lalala = regularextest.Replace("foo & bar", RSTest);

It's very frustrating because google replaces the string with an & or "AND" word.

Thanks in advance

This \\b&\\b will not match because & and ; are not word characters.

You could try this :

Regex regularextest = new Regex("(?<=^|\s+)&amp;(?=\s+|$)", options);

If you need to identify/convert valid entities (non-unicode), you could use this regex
(?:&(?:[A-Za-z_:][\\w:.-]*|\\#(?:[0-9]+|x[0-9a-fA-F]+));)
to identify a possible value to replace, pass it to a callback function that further process the entity you wish to replace. This way it could all be done in a single regex global replace (with callback logic).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM