c# - Regular Expression - Match But Exclude? -
i have simple task of trying find , replace special characters within string. regex working there italics tags within string not want replace however; requirement me replace independent "<" , ">" characters causing italics tags morphed. there way me match special characters exclude italics pattern? here code:
string sampletext = "<i>this should in italics</i> ¶ character needs removed"; string spattern = "[―&<>♫♪–‧₢₳-⅓⅟□¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶•¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕ×ØÙÚÛÜÝÞßàáãäåæçèéêëìíîïðñòóôö÷øùüýþÿŒœŠšŸŽžƒ˜-‰›¢€°]"; string replacepattern = ""; string text = system.text.regularexpressions.regex.replace(sampletext, spattern, replacepattern, system.text.regularexpressions.regexoptions.ignorecase);
when program executes back:
ithis should in italics/i character needs removed
so possible me match special characters exclude italics tags? if not possible solution can think of removing italics tags string processing , validate result regex put italics tags in..
any ideas?
here's easy way:
string sampletext = "<i>this should in italics</i> ¶ character needs removed"; string spattern = "(</?i>)|[―&<>♫♪–‧₢₳-⅓⅟□¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶•¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕ×ØÙÚÛÜÝÞßàáãäåæçèéêëìíîïðñòóôö÷øùüýþÿŒœŠšŸŽžƒ˜-‰›¢€°]"; string replacepattern = "$1"; string text = regex.replace(sampletext, spattern, replacepattern, regexoptions.ignorecase); console.writeline(text); // <i>this should in italics</i> character needs removed
but work <i>
, </i>
tags. can expand other tags pretty (e.g. "(</?\w+>)|..."
simple tag without attributes) if more complicated that, i'd recommend parsing input xml first, , applying pattern text of nodes you're interested in.
Comments
Post a Comment