0

Consider the following:

string keywords = "(load|save|close)";
Regex x = new Regex(@"\b"+keywords+"\b");

I get no matches. However, if I do this:

Regex x = new Regex(@"\b(load|save|close)\b");

I get matches. How come the former doesn't work, and how can I fix this? Basically, I want the keywords to be configurable so I placed them in a string.

1
  • I see. I missed out something very obvious. Commented Oct 28, 2011 at 14:02

3 Answers 3

8

The last \b in the first code snippet needs a verbatim string specifier (@) in front of it as well as it is a seperate string instance.

string keywords = "(load|save|close)"; 
Regex x = new Regex(@"\b"+keywords+@"\b");  
Sign up to request clarification or add additional context in comments.

Comments

3

You're missing another verbatim string specifier (@ prefixed to the last \b):

Regex x = new Regex(@"\b" + keywords + @"\b");

Comments

2
Regex x = new Regex(@"\b"+keywords+@"\b");

You forgot additional @ before second "\b"

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.