.net - How to use C# regular expressions to emulate forum tags -
i building forum , want able use simple square bracket tags allow users format text. accomplishing parsing string , looking tags. it's tedious, when run tag [url=http://www.something.com]some text[/url]. having parse attribute, , value, , make sure has proper opening , closing tags kind of pain , seems silly. know how powerful regular expressions i'm not @ them , frustrate me no end.
any of regex gurus willing me out? think example me started. regex finding tags [b]bolded text[/b] , tags attributes link 1 listed above helpful. in advance!
edit: links laymen's terms tutorials regex helpful.
this should work. "=something.com" optional , accommodates single or double quotes , makes sure closing tag matches opening tag.
protected void page_load(object sender, eventargs e) { string input = @"my link: [url='http://www.something.com'][b]some text[/b][/url] awesome. jazz hands activate!!"; string result = parse(input); } //result: link: <a href="http://www.something.com"><b>some text</b></a> awesome. jazz hands activate!! private static string parse(string input) { string regex = @"\[([^=]+)[=\x22']*(\s*?)['\x22]*\](.+?)\[/(\1)\]"; matchcollection matches = new regex(regex).matches(input); (int = 0; < matches.count; i++) { var tag = matches[i].groups[1].value; var optionalvalue = matches[i].groups[2].value; var content = matches[i].groups[3].value; if (regex.ismatch(content, regex)) { content = parse(content); } content = handletags(content, optionalvalue, tag); input = input.replace(matches[i].groups[0].value, content); } return input; } private static string handletags(string content, string optionalvalue, string tag) { switch (tag.tolower()) { case "url": return string.format("<a href=\"{0}\">{1}</a>", optionalvalue, content); case "b": return string.format("<b>{0}</b>", content); default: return string.empty; } }
update
now i'm having fun this. cleaned bit , replaced " \x22 entire string can escaped per @brad christie's suggestion. regex won't break if there "[" or "]" characters in content. handles tags recursively
Comments
Post a Comment