Please provide a solution to write a regular expression as following in C#.NET: I would require a RegEx for Non-Alphabets(a to z;A to Z) and Non-Numerals(0 to 9开发者_JS百科). Mean to say as reverse way for getting regular expression other than alphabets and otherthan numerals(0 to 9).
Kindly suggest the solution for the same.
You can use a negated character class here:
[^a-zA-Z0-9]
Above regex will match a single character which can't be a latin lowercase or uppercase letter or a number.
The ^
at the start of the character class (the part between [
and ]
) negates the complete class so that it matches anything not in the class, instead of normal character class behavior.
To make it useful, you probably want one of those:
Zero or more such characters
[^a-zA-Z0-9]*
The asterisk (
*
) here signifies that the preceding part can be repeated zero or more times.One or more such characters
[^a-zA-Z0-9]+
The plus (
+
) here signifies that the preceding part can be repeated one or more times.A complete (possibly empty) string, consisting only of such characters
^[^a-zA-Z0-9]*$
Here the characters
^
and$
have a meaning as anchors, matching the start and end of the string, respectively. This ensures that the entire string consists of characters not in that character class and no other characters come before or after them.A complete (non-empty) string, consisting only of such characters
^[^a-zA-Z0-9]+$
Elaborating a bit, this won't (and can't) make sure that you won't use any other characters, possibly from other scripts. The string аеΒ
would be completely valid with the above regular expression, because it uses letters from Greek and Cyrillic. Furthermore there are other pitfalls. The string á
will pass above regular expression, while the string ́a
will not (because it constructs the letter á from the letter a and a combining diacritical mark).
So negated character classes have to be taken with care at times.
I can also use numerals from other scripts, if I wanted to: ١٢٣
:-)
You can use the character class
[^\p{L&}\p{Nd}]
if you need to take care of the above things.
just negate the class:
[^A-Za-z0-9]
To obey local setting use:
[^[:alnum:]]
精彩评论