tags:

views:

28

answers:

1

I need to find &#171 in a string with a regex. How would you add that into the following:

String RegExPattern = @"^[0-9a-df-su-z]+\.\s&#171";
Regex PatternRegex = new Regex(RegExPattern);
return (PatternRegex.Match(Source).Value);
+1  A: 

You should be able to simply use it directly:

var pattern = new Regex("&#171");

Of course, if used alone you can also use String.IndexOf instead. If you want to use it in another pattern, as in your question, go ahead. The usage is correct.

If, on the other hand, you also want to allow the named entity, use an alternation:

var pattern = new Regex("(?:&#171|«)");

Once again, the same can be done in a more complex expression. The ?: at the beginning of the group isn’t necessary; it just prevents that a capture group will be created for this alternation.

Konrad Rudolph