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

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -