Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to allow backslash in C#

Tags:

c#

regex

Can anyone provide me with regex for validating string which only should not allow any special characters except backslash. I tried

var regexItem = new Regex("^[a-zA-Z0-9\\\ ]*$");

But it doesn't seem to work

like image 888
Suhaib Avatar asked Feb 01 '13 07:02

Suhaib


2 Answers

Backslashes need to be escaped in regular expressions - and they also need to be escaped in C#, unless you use verbatim string literals. So either of these should work:

var regexItem = new Regex(@"^[a-zA-Z0-9\\ ]*$");
var regexItem = new Regex("^[a-zA-Z0-9\\\\ ]*$");

Both of these ensure that the following string content is passed to the Regex constructor:

^[a-zA-Z0-9\\ ]*$

The Regex code will then see the double backslash and treat it as "I really want to match the backslash character."

Basically, you always need to distinguish between "the string contents you want to pass to the regex engine" and "the string literal representation in the source code". (This is true not just for regular expressions, of course. The debugger doesn't help by escaping in Watch windows etc.)

EDIT: Now that the question has been edited to show that you originally had three backslashes, that's just not valid C#. I suspect you were aiming for "a string with three backslashes in" which would be either of these:

var regexItem = new Regex(@"^[a-zA-Z0-9\\\ ]*$");
var regexItem = new Regex("^[a-zA-Z0-9\\\\\\ ]*$");

... but you don't need to escape the space as far as the regular expression is concerned.

like image 194
Jon Skeet Avatar answered Nov 18 '22 01:11

Jon Skeet


You either need to double escape it (once for C# and once for the Regex engine):

var regexItem = new Regex("^[a-zA-Z0-9\\\\ ]*$");

Or you can use the verbatim string feature of C# (note the @):

var regexItem = new Regex(@"^[a-zA-Z0-9\\ ]*$");

In a verbatim string, the backslash is not interpreted as starting an escape sequence, so you just need to escape it once for the Regex engine.

I assume that your current code doesn't compile. It should say something along the lines of "Unrecognized escape sequence".
The reason for this is that you have three backslashes followed by a space. The first two backslashes are interpreted as an escape sequence representing a backslash, but the third backslash is interpreted as starting an escape sequence with a space as the second character. Such an escape sequence doesn't exist, leading to the error.

like image 11
Daniel Hilgarth Avatar answered Nov 18 '22 00:11

Daniel Hilgarth