c# - Replacing the character between angle brackets -
story: have list box shows methods of current application. need colorize method parameters' data type blue.
solution: first, extract content between parenthesis. second, want split them comma
problem: if arguments entail idictionary<string, string>
occurs multiple times, above solution faces problem!! thus, decided first grab content between angle brackets , replace comma "#comma#"
, after performing task using above solution replace "#comma#"
",". but, based on solution found here, not possible set value match.value
. here code:
if (methodargumenttype.contains("<") && methodargumenttype.contains(">")) { var regex = new regex("(?<=<).*?(?=>)"); foreach (match match in regex.matches(methodargumenttype)) { match.value = match.value.replace(",", "#comma#"); } }
any suggestion highly appreciated.
you need replace matched value inside match evaluator within regex.replace
:
var methodargumenttype = "idictionary<string, string>"; if (methodargumenttype.contains("<") && methodargumenttype.contains(">")) { methodargumenttype = regex.replace(methodargumenttype, @"<([^<>]+)>", m => string.format("<{0}>", m.groups[1].value.replace(",", "#comma#"))); } console.writeline(methodargumenttype); // => idictionary<string#comma# string>
here, m.groups[1].value
hold string, string
, replacement done on input string itself, not regex.match
object.
Comments
Post a Comment