Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POSIX character equivalents in Java regular expressions

I would like to use a regular expression like this in Java : [[=a=][=e=][=i=]].

But Java doesn't support the POSIX classes [=a=], [=e=] etc.

How can I do this? More precisely, is there a way to not use US-ASCII?

like image 360
Stephan Avatar asked Jul 07 '11 15:07

Stephan


2 Answers

Java does support posix character classes. The syntax is just different, for instance:

\p{Lower}
\p{Upper}
\p{ASCII}
\p{Alpha}
\p{Digit}
\p{Alnum}
\p{Punct}
\p{Graph}
\p{Print}
\p{Blank}
\p{Cntrl}
\p{XDigit}
\p{Space}
like image 106
Johan Sjöberg Avatar answered Oct 09 '22 13:10

Johan Sjöberg


Quoting from http://download.oracle.com/javase/1.6.0/docs/api/java/util/regex/Pattern.html

POSIX character classes (US-ASCII only)

\p{Lower}   A lower-case alphabetic character: [a-z]
\p{Upper}   An upper-case alphabetic character:[A-Z]
\p{ASCII}   All ASCII:[\x00-\x7F]
\p{Alpha}   An alphabetic character:[\p{Lower}\p{Upper}]
\p{Digit}   A decimal digit: [0-9]
\p{Alnum}   An alphanumeric character:[\p{Alpha}\p{Digit}]
\p{Punct}   Punctuation: One of !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
\p{Graph}   A visible character: [\p{Alnum}\p{Punct}]
\p{Print}   A printable character: [\p{Graph}\x20]
\p{Blank}   A space or a tab: [ \t]
\p{Cntrl}   A control character: [\x00-\x1F\x7F]
\p{XDigit}  A hexadecimal digit: [0-9a-fA-F]
\p{Space}   A whitespace character: [ \t\n\x0B\f\r]
like image 33
ahmet alp balkan Avatar answered Oct 09 '22 14:10

ahmet alp balkan