-3

I would like to replace all the occurrences of a string, as long as those don't start by '@', so for example in the following query:

 (surname = @surname and surname = @surname1)

if I want to replace surname, it will only replace the two of them on the left side of the equal sign. Thus, leaving @surname and @surname1 unreplaced.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rafael
  • 1,099
  • 5
  • 23
  • 47

1 Answers1

3

You can use Regex.Replace for this:

Regex.Replace(yourString, "([^@])surname", "$1Diaz");

The [^@] basically tells Regex that any character except the @ symbol can come in front of the "surname" text that you are looking for. The $1 is necessary because otherwise, whatever character that is will also get stripped out.

Note that this Regex, without some additional modification, will not match "surname" if it is at the beginning of the string. In the example you provided, it starts with an open-parenthesis, so as long as that condition holds, the solution above will work.

Michael Bray
  • 14,998
  • 7
  • 42
  • 68