XML technologies / Regular expressions / Java / The regex dialect of Java

(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)

java.util.regex uses a traditional NFA, so the discussions under How regex engines work, Practical regex techniques and The art of writing regular expressions also apply. The table summarizes supported metacharacters. Options in functions and factory methods control some dialect features; modifiers can also be enabled or disabled within a regex using (?mod-mod) or (?mod-mod:...). Supported modes appear in the table Regex modes in java.util.regex.

Table: Overview of regex elements in Sun’s java.util.regex.

Character shorthands 1.

see Shorthand metacharacters

(K)

\a [\b] \e \f \n \r \t \Ooktal \x## \u#### \cZeichen

Character classes and related constructs

see Ordinary classes: [a-z] and [^a-z]

(K)

Classes: [...] [^...] (may contain set operations; see Character-class set operations: [[a-z]&&[^aeiou]])

see Almost any character: dot

  

Almost any character: dot (meaning depends on the mode)

see Character-class shorthands

(K)

Character-class shorthands:2. \w \d \s \W \D \S

see Unicode properties, scripts, and blocks

(K)

Unicode properties and blocks:3. \p{Prop} \P{Prop}

Anchors and other zero-width assertions

see Start of line, start of string

  

Start of line or string: ^ \A

see End of line, end of string

  

End of line or string: $ \z \Z

see Start of the next search or end of the previous match: \G

  

Start of the current match: \G

see Word boundaries

  

Word boundary:4. \b \B

see Lookahead; lookbehind

  

Lookaround:5. (?=...) (?!...) (?<=...) (?<!...)</td>

Comments and mode modifiers

see Mode modifiers

  

Mode modifiers: (?mod-mod) Allowed modifiers: x d s m i u

see Scoped mode modifiers

  

Scope for mode modifiers: (?mod-mod:...)

see The # sequence

(K)

Comments: from # to the line’s end, only when enabled6.

see Literal-text mode

(K)

Literal-text mode:7. \Q...\E

Grouping and capturing text

see Grouping and capturing parentheses

  

Capturing parentheses: (...) \1 \2 ...

see Noncapturing grouping parentheses

  

Noncapturing grouping parentheses: (?:...)

see Atomic parentheses

  

Atomic groups: (?>...)

see Alternation

  

Alternation: |

see Greedy quantifiers

  

Greedy quantifiers: * + ? {n} {n,} {x,y}

see Lazy quantifiers

  

Lazy quantifiers: *? +? ?? {n}? {n,}? {x,y}?

see Possessive quantifiers

  

Possessive quantifiers: *+ ++ ?+ {n}+ {n,}+ {x,y}+

(K) – usable inside a character class.

1....7. see text

Notes on the table:

  1. The metacharacter \b represents backspace only inside character classes; outside, \b marks word boundaries (see Word boundaries).

    The table shows bare backslashes, not the doubled forms needed in string literals. Within a string, the table’s ˹\n˼ must be written as "\\n". See Strings as regular expressions.

    With \x##, exactly two hexadecimal digits are required, so ˹\xFCber˼ matches ›über‹.

    With \u####, exactly four hexadecimal digits are required, so ˹\u00FCber˼ matches ›über‹ and ˹\u20AC˼ matches the € character.

    With \0oktal, the leading 0 is mandatory, followed by one to three octal digits, for example \0134 for backslash.

    With \cZeichen, uppercase and lowercase oddly differ: the ASCII code is blindly combined with 64 using xor. Unlike every other implementation I know, \cA and \ca differ. \cA is the usual Ctrl-A, so uppercase letters are normally appropriate. \ca, however, is equivalent to \x21 and matches ›!‹.

  2. \w, \d and \s , and their uppercase variants, cover ASCII only, not Unicode’s extended alphanumerics, digits, or whitespace. Thus, \d is exactly [0-9], \w is equivalent to [0-9a-zA-Z_], and \s is equivalent to [●\t\n\f\r\x0B]. The \x0B is ASCII’s rarely used VT character.

    Use Unicode properties for equivalents: \p{L} for \w, \p{Nd} for \d and \p{Z} for \s, with the \P{...} versions for \W, \D and \S.

  3. \p{...} and \P{...} support most standard Unicode properties and blocks, plus Java properties. Scripts are unsupported. Details follow.

  4. The word-boundary definition of \b and \B disagrees with the word-character definition of \w and \W. The boundary metacharacters \b and \B examine Unicode properties on either side, whereas \w and \W are ASCII-only.

  5. Lookahead constructs allow arbitrarily complex regexes; lookbehind permits only regexes with a predetermined maximum match length. Thus, ˹?˼ is allowed in lookbehind, but not ˹*˼ and ˹+˼. This restriction was explained under Features and dialects in Lookahead; lookbehind.

  6. The sequence #...NL is a comment only when the option Pattern.COMMENTS or modifier x is enabled; see Regex modes in java.util.regex. Remember the final newline in multiline strings; see the example under Processing CSV data.

    Unescaped ASCII whitespace is ignored. Caution: unlike most languages with regex comments, comments and whitespace are also allowed inside character classes.

  7. \Q...\E has been supported from the start, but was unreliable inside character classes before Java 1.6.

     

 

Table: Regex modes in java.util.regex.

Compile-time option

(?mod)

Description

Pattern.UNIX_LINES

d

Affects how ˹.˼ and ˹^˼ match (see Different interpretations of ˹.˼, ˹^˼, ˹$˼ and ˹\Z˼).

Pattern.DOTALL

s

Makes dot match every character (see Dot-matches-all mode).

Pattern.MULTILINE

m

˹^˼ and ˹$˼ also match internal line boundaries (see Unicode line terminators).

Pattern.COMMENTS

x

Free-spacing mode and regex comments (see Username and hostname), also inside character classes.

Pattern.CASE_INSENSITIVE

i

Ignores ASCII case.

Pattern.UNICODE_CASE

u

Ignores case for all characters.

Pattern.CANON_EQ

Unicode canonical equivalence: equivalent characters match despite different encodings (see Multiple code points for one character).

Pattern.LITERAL

u

Treats the argument as fixed text rather than a regex.

Java support for \p{...} and \P{...}

The constructs ˹\p{...}˼ and ˹\P{...}˼ support Unicode properties, blocks, and Java-specific properties. Unicode 4.0.0 is supported; Java 1.4.2 and earlier support only Unicode 3.0.0.

  

  

 

 

 

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