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. | |||
|---|---|---|---|
(K) |
| ||
Character classes and related constructs | |||
(K) | Classes: | ||
| Almost any character: dot (meaning depends on the mode) | ||
(K) | Character-class shorthands:2. | ||
(K) | Unicode properties and blocks:3. | ||
Anchors and other zero-width assertions | |||
| Start of line or string: | ||
| End of line or string: | ||
see Start of the next search or end of the previous match: \G |
| Start of the current match: | |
see Word boundaries |
| Word boundary:4. | |
| Lookaround:5. | ||
Comments and mode modifiers | |||
see Mode modifiers |
| Mode modifiers: | |
| Scope for mode modifiers: | ||
see The # sequence | (K) | Comments: from | |
(K) | Literal-text mode:7. | ||
Grouping and capturing text | |||
| Capturing parentheses: | ||
| Noncapturing grouping parentheses: | ||
| Atomic groups: | ||
see Alternation |
| Alternation: | | |
| Greedy quantifiers: | ||
see Lazy quantifiers |
| Lazy quantifiers: | |
| Possessive quantifiers: | ||
(K) – usable inside a character class. | 1....7. see text | ||
Notes on the table:
The metacharacter
\brepresents backspace only inside character classes; outside,\bmarks 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 leading0is mandatory, followed by one to three octal digits, for example\0134for backslash.With
\cZeichen, uppercase and lowercase oddly differ: the ASCII code is blindly combined with 64 using xor. Unlike every other implementation I know,\cAand\cadiffer.\cAis the usual Ctrl-A, so uppercase letters are normally appropriate.\ca, however, is equivalent to\x21and matches›!‹.\w, \dand\s, and their uppercase variants, cover ASCII only, not Unicode’s extended alphanumerics, digits, or whitespace. Thus,\dis exactly[0-9],\wis equivalent to[0-9a-zA-Z_], and\sis equivalent to[●\t\n\f\r\x0B]. The\x0Bis ASCII’s rarely used VT character.Use Unicode properties for equivalents:
\p{L}for\w,\p{Nd}for\dand\p{Z}for\s, with the\P{...}versions for\W, \Dand\S.\p{...}and\P{...}support most standard Unicode properties and blocks, plus Java properties. Scripts are unsupported. Details follow.The word-boundary definition of
\band\Bdisagrees with the word-character definition of\wand\W. The boundary metacharacters\band\Bexamine Unicode properties on either side, whereas\wand\Ware ASCII-only.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.The sequence
#...NLis a comment only when the optionPattern.COMMENTSor modifierxis 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.
\Q...\Ehas 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 |
| Description |
|---|---|---|
|
| Affects how |
|
| Makes dot match every character (see Dot-matches-all mode). |
|
|
|
|
| Free-spacing mode and regex comments (see Username and hostname), also inside character classes. |
|
| Ignores ASCII case. |
|
| Ignores case for all characters. |
| Unicode canonical equivalence: equivalent characters match despite different encodings (see Multiple code points for one character). | |
|
| 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