XML technologies / Regular expressions / Java / Java’s regex dialect / Unicode properties

Unicode properties use short names such as \p{Lu} (see the table Basic Unicode subproperties). Single-letter property names may omit the braces: \pL is the same as \p{L}. Long names such as \p{Lowercase_Letter} are unsupported.

In Java 1.5 and earlier, the properties Pi and Pf were unsupported, so characters with these properties were not matched by \p{P}. They are supported from Java 1.6 onward.

The Other property, \p{C}, does not match code points in the unassigned category, \p{Cn}.

The composite property \p{L&} is unsupported.

The property \p{all} is supported and equivalent to (?s:.). However, neither \p{assigned} nor \p{unassigned} is supported; use \P{Cn} and \p{Cn} instead.

 

Unicode blocks

Block names must include the prefix ›In‹. Unicode differences between Java 1.4.2 and 1.5.0 discusses differences in block names inside \p{...} and \P{...} across Java versions.

Two Unicode blocks changed names between Unicode 3.0 and 4.0: Combining Marks for Symbols became Combining Diacritical Marks for Symbols and Greek became Greek and Coptic. Java 1.5 accepts both old and new names.

Java 1.4.2 had a bug involving the Unicode block names Arabic Presentation Forms-B and Latin Extended-B; Java 1.5 fixes it (see also Unicode differences between Java 1.4.2 and 1.5.0).

 

Special Java properties

From Java 1.5, the constructs \p{...} and \P{...} can also use the isIrgendwas methods from java.lang.Character, though obsolete names are not retroactively supported. To use one in a regex, replace the method-name prefix ›is‹ with ›java‹ and use that name inside ˹\p{...}˼ or ˹\P{...}˼. For example, the regex ˹\{javaJavaIdentifierStart}˼ matches the same characters as the Java method java.lang.Character.isJavaIdentifierStart. The documentation for java.lang.Character lists the corresponding methods.

 

Unicode line endings

Before Unicode, most regex dialects treated newline specially with respect to ˹.˼, ˹^˼ , ˹$˼ and ˹\Z˼. Unicode introduces a larger set of possible line-ending characters (see Line endings in Unicode).

Java normally treats these characters as line endings:

Character code

Name

Description

U+000A

LF

\n

ASCII line feed

U+000D

CR

\r

ASCII carriage return

U+000D U+000A

CR/LF

\r\n

ASCII carriage return / line feed, CRLF

U+0085

NEL

Unicode NEXT LINE

U+2028

LS

Unicode LINE SEPARATOR

U+2029

PS

Unicode PARAGRAPH SEPARATOR

 

Depending on regex mode (see Regex modes in java.util.regex), ˹.˼, ˹^˼, ˹$˼ and ˹\Z˼ are interpreted differently:

Regex mode

affects

Description

UNIX_LINES

^ . $ \Z

Traditional behavior: only newline, LF, ends a line.

MULTILINE

^ $

^ and $ also match line endings within the subject string.

DOTALL

.

Line endings do not affect .; it matches every character.

CR/LF deserves special attention. Normally, when UNIX_LINES is not used, line-ending metacharacters treat CR/LF as a unit and can never match between CR and LF.

For example, $ and \Z normally match immediately before a line ending. LF qualifies, but its preceding position matches only when it is not part of CR/LF, meaning it is not preceded by CR.

This also affects ^ and $ in MULTILINE mode. Here, ^ also matches immediately after CR within the string, but only if LF does not follow. Likewise, $ matches immediately before LF within the string only if CR does not precede it.

DOTALL , however, does not affect CR/LF handling (DOTALL affects only dot, ›dot‹, treating characters of such a sequence individually). With UNIX_LINES this no longer matters, since line-ending characters are treated like all other characters by dot.

  

 

 

 

German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.

O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln

back to top