Reputation: 33
In c#, I want use a regular expression to replace each variable @A with a number withouth replacing other similar variables like @AB
string input = "3*@A+3*@AB/@A";
string value = "5";
string pattern = "@A"; //<- this doesn't work
string result = Regex.Replace(input, pattern, value);
// espected result = "3*5+3*@AB/5"
any good idea?
Upvotes: 3
Views: 652
Reputation: 627128
Use a word boundary \b
:
string pattern = @"@A\b";
See regex demo (Context tab)
Note the @
before the string literal: I am using a verbatim string literal to declare the regex pattern so that I do not have to escape the \
. Otherwise, it would look like string pattern = "@A\\b";
.
Upvotes: 1