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

mysql - FireDac error 314 - but DLLs are in program directory -

git - How to list all releases of public repository with GitHub API V3 -

c++ - Getting C2512 "no default constructor" for `ClassA` error on the first parentheses of constructor for `ClassB`? -