The following snippet prints False
:
Console.WriteLine(Regex.IsMatch("abc", @"[[:alpha:]]"));
But this prints True
:
Console.WriteLine(Regex.IsMatch("abc", @"[a-zA-Z]"));
Why? Shouldn't they be equivalent?
There are three categories of functions for POSIX-style regular expressions: matching, replacing, and splitting.
In the context of regular expressions, a character class is a set of characters enclosed within square brackets. It specifies the characters that will successfully match a single character from a given input string.
1 BREs Matching a Single Character or Collating Element. A BRE ordinary character, a special character preceded by a <backslash>, or a <period> shall match a single character. A bracket expression shall match a single character or a single collating element.
Short for regular expression, a regex is a string of text that lets you create patterns that help match, locate, and manage text. Perl is a great example of a programming language that utilizes regular expressions. However, its only one of the many places you can find regular expressions.
.NET Regexes don't support the Posix character classes. They do however support Unicode groups.
This would work:
Regex.IsMatch("abc", @"^\p{L}+$");
The \p{L}
group matches all Unicode letters.
See here for more information:
http://msdn.microsoft.com/en-us/library/20bw873z.aspx#CategoryOrBlock
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With