views:

70

answers:

1

Hi,

I use this regular expression for validating some of my textbox :

Regex re = new Regex("^([äö\x20\x27\x2C\x2D\x5Fò-öà-âù-üç-ï0-9a-zA-Z]+)$");

And when i put "<" or ">" in one of this textBox, my condition shoud be false and not true :)

if (re.IsMatch(TextBox.Text)) /*do something */ else Console.write("error invalid char in textbox")

That's so weird because if i test it with RegexBuddy this two char are not valid, so if someone can help me ;)

Regards,

Cytemax

+3  A: 

Try this:

Regex re = new Regex(@"^([äö\x20\x27\x2C\x2D\x5Fò-öà-âù-üç-ï0-9a-zA-Z]+)$");

If you omit the @ it means the string will not actually contain the slashes. Alternatively you can double up on the slashes.

Cobusve
Tested - works.
Ed
It seems to be working ;)tks ^^What does this @ meens?Ok tks.
Cytemax
Cytemax, the @ means a "verbatim string literal", which tells the C# parser to not interpret any \s, new lines, etc., and to simply make them part of the string.
Warren
It means that the string is a literal string (does not contain escape characters). When you omit that the string is changed by the escapes you have added before it reaches the RegExp engine.E.g. the string "\x20" is in fact equivalent to @" ", while "\\x20" is equivalent to @"\x20"
Cobusve
Thanks all ;) - Awesome website !!
Cytemax