Regular expression query
I am looking for regular expression that evaluates below开发者_JAVA技巧.
0-9 a-Z A-Z - / '
The C# version of this pattern is:
@"[0-9a-zA-Z/'-]"
Used in code:
var regex = new Regex(@"[0-9a-zA-Z/'-]");
or
var regex = new Regex(@"[0-9a-z/'-]", RegexOptions.IgnoreCase);
Note that the -
is at the very end of the character class (the part in the brackets). For -
to mean a literal hyphen inside a character class, it must be at the beginning or end of the class (i.e. [-blah]
or [blah-]
), or escaped with a backslash: [ab\-c]
will match a
, b
, c
, or -
.
Note also the @
at the beginning of the quoted string. This isn't important for this pattern, but it's a good habit to get into with C# regex. Regular expressions often contain backslashes, and the @"..."
form will allow you to use backslashes in your pattern without having to escape them.
Use bellow code to validate(Regex patterns) Alphabetic and numbers:
String name="123ABCabc";
if(System.Text.RegularExpressions.Regex.Match(name, @"[0-9a-zA-Z_]") == true)
{
return true;
}
else
{
return false;
}
In the case you want to match digits, lower- and higher-case latin characters, "-", "/" and "'" then I would suggest the following:
[0-9a-zA-Z-\/\']
精彩评论