Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I match POSIX Character Classes

Tags:

.net

regex

posix

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?

like image 960
dlras2 Avatar asked Mar 10 '12 03:03

dlras2


People also ask

Which of the following is POSIX style regular expression?

There are three categories of functions for POSIX-style regular expressions: matching, replacing, and splitting.

What are character classes in regex?

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.

What BRE would match a single character on only a single character?

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.

What does this regex do?

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.


1 Answers

.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

like image 200
gymbrall Avatar answered Sep 23 '22 21:09

gymbrall