0

This code doesn't work, but it works with other expressions, like (?:[A-Za-z][A-Za-z0-9_]*).

The below expression works correctley in a regular expression tester, but it doesn't replace Hello with id in this code:

string test = "int Hello :=   2 ;";
string pattern = "\b(?!int|bool)(?:[A-Za-z][A-Za-z0-9_]*)\b";
string replacement = "Id";
Regex rgx = new Regex(pattern);
string newline = rgx.Replace(test, replacement);

1 Answer 1

4

You should escape backslashes or use @ beginning of your string and make it verbatim string. \b has a special meaning in C# which is backspace, see documentation: Escape Sequences

string pattern = @"\b(?!int|bool)(?:[A-Za-z][A-Za-z0-9_]*)\b";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.