c# - How to replace a user input in regular expression? -
i use simple codes describe situation. example, here codes:
using system; using system.text.regularexpressions; public class example { public static void main() { string pattern = @"\b(?!non)\w+\b"; string input = "nonsense not non-functional."; foreach (match match in regex.matches(input, pattern, regexoptions.ignorecase)) console.writeline(match.value); } }
now, replace "non" user input. let it's called "userinput" , codes written getting user input. want this, error exists:
string pattern = @"\b(?!{0})\w+\b", userinput;
is there way replace "non" inside pattern of regex user input?
i think you're missing string.format()
:
string pattern = string.format(@"\b(?!{0})\w+\b", userinput);
Comments
Post a Comment