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 <<vs_user_name>></div><div> </div><div>full name <<vs_user_full_name>></div><div> </div><div>password <<vs_user_password>></div><div> </div><div>thanks</div>
code:
static void main(string[] args) { string text = "<div>username <input class=\"vsfield\" contenteditable=\"false\" name=\"vs_user_name\" style=\"background-color: rgb(220,220,200);\">[user name]</input></div><div> </div><div>full name <input class=\"vsfield\" contenteditable=\"false\" name=\"vs_user_full_name\" style=\"background-color: rgb(220,220,200);\">[full name]</input></div><div> </div><div>password <input class=\"vsfield\" contenteditable=\"false\" name=\"vs_user_password\" style=\"background-color: rgb(220,220,200);\">[password]</input></div><div> </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
Post a Comment