c# - Regex - how to do html tag name replacement token -


i trying token replacement in html untokenised string has multiple <input></input> tags. want replace name attribute token <<vs_user_name>> example. regex replaces <input> regardless. below stand alone example.

this desired output

<div>username&nbsp;<<vs_user_name>></div><div>&nbsp;</div><div>full name&nbsp;<<vs_user_full_name>></div><div>&nbsp;</div><div>password&nbsp;<<vs_user_password>></div><div>&nbsp;</div><div>thanks</div> 

code:

static void main(string[] args)     {         string text = "<div>username&nbsp;<input class=\"vsfield\" contenteditable=\"false\" name=\"vs_user_name\" style=\"background-color: rgb(220,220,200);\">[user name]</input></div><div>&nbsp;</div><div>full name&nbsp;<input class=\"vsfield\" contenteditable=\"false\" name=\"vs_user_full_name\" style=\"background-color: rgb(220,220,200);\">[full name]</input></div><div>&nbsp;</div><div>password&nbsp;<input class=\"vsfield\" contenteditable=\"false\" name=\"vs_user_password\" style=\"background-color: rgb(220,220,200);\">[password]</input></div><div>&nbsp;</div><div>thanks</div>";         string texttokenised = gettokenisedtext(text, "vs_user_name", "vs_user_full_name", "vs_user_password");     }  private static string gettokenisedtext(string untokenised, params string[] tokenkeys)     {         foreach (string tokenkey in tokenkeys)         {             string string2 = gettoken(tokenkey);             string string1 = getregex(tokenkey);              untokenised = regex.replace(untokenised, string1, string2);         }          return untokenised;     }       private static string gettoken(string tokenkey)     {         return string.format("<<{0}>>", tokenkey);     }       private static string getregex(string tokenkey)     {         return string.format("()<input([^>]*e*)name=\"{0}\"([^>]*e*)>(.*)</input>", tokenkey);                 } 

your regex greedy default .*.. have make non greedy adding ?. use following:

return string.format("()<input([^>]*e*)name=\"{0}\"([^>]*e*)>(.*?)</input>", tokenkey);                                                                  ↑ 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -