The character x
The most basic form of pattern matching supported Java regular-expressions is the match of a string literal. For example, if the regular expression isfoo
, and the input string isfoo
, the match will succeed because these strings are identical.This match was a success. Note that while the input string is 3 characters long, the start index is 0 and the end index is 3. By convention, ranges are inclusive of the beginning index and exclusive of the end index. With subsequent matches, you'll notice some overlap; the start index for the next match is the same as the end index of the previous match:Current REGEX is: foo Current INPUT is: foo Finds the text "foo" starting at index 0 and ending at index 3.Current REGEX is: foo Current INPUT is: foofoofoo Finds the text "foo" starting at index 0 and ending at index 3. Finds the text "foo" starting at index 3 and ending at index 6. Finds the text "foo" starting at index 6 and ending at index 9.Explanation - Metacharacters
Java regular-expressions also support a number of special characters which can affect the way a pattern is matched.The match still succeeds, even though the period (.) is not present in the input string. It succeeds because the period is a metacharacter--a character with special meaning interpreted by the matcher. The metacharacter "." means "any character" which is why the match in our example succeeds.Current REGEX is: cat. Current INPUT is: cats Finds the text "cats" starting at index 0 and ending at index 4.The metacharacters supported are:
([{\^$|)?*+.
Note: In certain situations the special characters listed above will not be treated as metacharacters. You'll encounter this as you learn more about how regular expressions are constructed. You can, however, use this list to check whether or not a specific character will ever be considered a metacharacter. For example, the characters!
@
and#
never carry a special meaning.There are two ways to force a metacharacter to be treated as an ordinary character:
When using this technique, the
- precede the metacharacter with a backslash, or
- enclose it within
\Q
(which starts the quote) and\E
(which ends it).\Q
and\E
can be placed at any location within the expression, provided that the\Q
comes first.
Perl constructs not supported by Java: