c# - replace variable name in formula with Regex.Replace -
in c#, want use regular expression replace each variable @a number withouth replacing other similar variables @ab
string input = "3*@a+3*@ab/@a"; string value = "5"; string pattern = "@a"; //<- doesn't work string result = regex.replace(input, pattern, value); // espected result = "3*5+3*@ab/5"
any idea?
use word boundary \b
:
string pattern = @"@a\b";
see regex demo (context tab)
note @
before string literal: using verbatim string literal declare regex pattern not have escape \
. otherwise, string pattern = "@a\\b";
.
Comments
Post a Comment