regular expression

Regex regex = new Regex( "^\d+$");

this code gives an error pointing \d .

error message is "invalid escape character" , but in fact \d is not escap character , \d means 0 to 9 digits.

how can i fix this

thnx

[328 byte] By [SyedRahmanMashwani] at [2008-2-7]
# 1
It is an escape character to C#, not regex try this:

new Regex(@"^\d+$");

The @ tells the C# compiler to treat all within quotes as a literal and not do escapes.

OmegaMan at 2007-8-30 > top of Msdn Tech,.NET Development,Regular Expressions...
# 2

In a regular expression "\d" means digit. However, the trick is getting that to the regular expression.

To the C# compile, inside a string \d means "escaped-D" (which is meaningless). There are two ways to deal with this:

escape the back-slash: Regex("^\\d+$"); (Escaped-backslash means just backslash)

use a literal string: Regex(@"^\d+$"); (@ means that inside that string nothing has a special meaning (except double-quote))

JamesCurran at 2007-8-30 > top of Msdn Tech,.NET Development,Regular Expressions...
# 3
tnx alot ,both of u.
SyedRahmanMashwani at 2007-8-30 > top of Msdn Tech,.NET Development,Regular Expressions...

.NET Development

Site Classified