Java : Pattern (regex) with Examples

Pattern (Java SE 22 & JDK 22) with Examples.
You will find code examples on most Pattern methods.


Summary

A compiled representation of a regular expression.

Class diagram

// Tests if this pattern matches a given input string.
final var pattern = Pattern.compile("a+bc");
final var matchPredicate = pattern.asMatchPredicate();

System.out.println(matchPredicate.test("abc")); // true
System.out.println(matchPredicate.test("aaaabc")); // true

System.out.println(matchPredicate.test("ab")); // false
System.out.println(matchPredicate.test("abcD")); // false
System.out.println(matchPredicate.test("Zabc")); // false
// Tests if this pattern is found in a given input string.
final var pattern = Pattern.compile("a+bc");
final var predicate = pattern.asPredicate();

System.out.println(predicate.test("abc")); // true
System.out.println(predicate.test("aaaabc")); // true

System.out.println(predicate.test("ab")); // false
System.out.println(predicate.test("abcD")); // true
System.out.println(predicate.test("Zabc")); // true
// An examples with a matcher.
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("xyz aaaabc 012");

if (matcher.find()) {
    System.out.println(matcher.group()); // "aaaabc"
}

Fields

static final int CANON_EQ

Enables canonical equivalence.

final var regex = "a\u030A";
System.out.println(regex); // å

final var input = "\u00E5";
System.out.println(input); // å

{
    final var pattern = Pattern.compile(regex, Pattern.CANON_EQ);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input)); // true
}

{
    final var pattern = Pattern.compile(regex);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input)); // false
}

static final int CASE_INSENSITIVE

Enables case-insensitive matching.

final var regex = "abc";
final var input = "ABC";

{
    final var pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input)); // true
}

{
    final var pattern = Pattern.compile(regex);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input)); // false
}

static final int COMMENTS

Permits whitespace and comments in pattern.

final var regex = "a b c # xyz";
final var input = "abc";

{
    final var pattern = Pattern.compile(regex, Pattern.COMMENTS);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input)); // true
}

{
    final var pattern = Pattern.compile(regex);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input)); // false
}

static final int DOTALL

Enables dotall mode.

final var regex = "abc..yz";
final var input = "abc\nxyz";

{
    final var pattern = Pattern.compile(regex, Pattern.DOTALL);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input)); // true
}

{
    final var pattern = Pattern.compile(regex);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input)); // false
}

static final int LITERAL

Enables literal parsing of the pattern.

final var regex = "abc+";
final var input1 = "abc+";
final var input2 = "abccc";

{
    final var pattern = Pattern.compile(regex, Pattern.LITERAL);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input1)); // true
    System.out.println(matchPredicate.test(input2)); // false
}

{
    final var pattern = Pattern.compile(regex);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input1)); // false
    System.out.println(matchPredicate.test(input2)); // true
}

static final int MULTILINE

Enables multiline mode.

final var regex = "^abc$";
final var input1 = """
        012
        abc
        xyz
        """;
final var input2 = "abc";

{
    final var pattern = Pattern.compile(regex, Pattern.MULTILINE);
    final var predicate = pattern.asPredicate();

    System.out.println(predicate.test(input1)); // true
    System.out.println(predicate.test(input2)); // true
}

{
    final var pattern = Pattern.compile(regex);
    final var predicate = pattern.asPredicate();

    System.out.println(predicate.test(input1)); // false
    System.out.println(predicate.test(input2)); // true
}

static final int UNICODE_CASE

Enables Unicode-aware case folding.

// Full-width characters.
final var regex = "abc";
final var input = "ABC";

{
    final var pattern = Pattern.compile(regex,
            Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input)); // true
}

{
    final var pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input)); // false
}

static final int UNICODE_CHARACTER_CLASS

Enables the Unicode version of Predefined character classes and POSIX character classes.

final var regex = "\\p{Lower}+";

// Half-width characters.
final var input1 = "abc";

// Full-width characters.
final var input2 = "abc";

{
    final var pattern = Pattern.compile(regex, Pattern.UNICODE_CHARACTER_CLASS);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input1)); // true
    System.out.println(matchPredicate.test(input2)); // true
}

{
    final var pattern = Pattern.compile(regex);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input1)); // true
    System.out.println(matchPredicate.test(input2)); // false
}

static final int UNIX_LINES

Enables Unix lines mode.

final var regex = "^abc$";

final var input1 = "abc\r\nxyz";
final var input2 = "abc\nxyz";
final var input3 = "abc\rxyz";

{
    final var pattern = Pattern.compile(regex,
            Pattern.UNIX_LINES | Pattern.MULTILINE);
    final var predicate = pattern.asPredicate();

    System.out.println(predicate.test(input1)); // false
    System.out.println(predicate.test(input2)); // true
    System.out.println(predicate.test(input3)); // false
}

{
    final var pattern = Pattern.compile(regex, Pattern.MULTILINE);
    final var predicate = pattern.asPredicate();

    System.out.println(predicate.test(input1)); // true
    System.out.println(predicate.test(input2)); // true
    System.out.println(predicate.test(input3)); // true
}

Methods

Predicate<String> asMatchPredicate ()

Creates a predicate that tests if this pattern matches a given input string.

final var pattern = Pattern.compile("a+bc");

final var matchPredicate = pattern.asMatchPredicate();
final var predicate = pattern.asPredicate();

System.out.println(matchPredicate.test("aaabc")); // true
System.out.println(predicate.test("aaabc")); // true

System.out.println(matchPredicate.test("012 abc xyz")); // false
System.out.println(predicate.test("012 abc xyz")); // true

Predicate<String> asPredicate ()

Creates a predicate that tests if this pattern is found in a given input string.

final var pattern = Pattern.compile("a+bc");

final var matchPredicate = pattern.asMatchPredicate();
final var predicate = pattern.asPredicate();

System.out.println(matchPredicate.test("aaabc")); // true
System.out.println(predicate.test("aaabc")); // true

System.out.println(matchPredicate.test("012 abc xyz")); // false
System.out.println(predicate.test("012 abc xyz")); // true

static Pattern compile (String regex)

Compiles the given regular expression into a pattern.

final var pattern = Pattern.compile("a+bc");
final var matchPredicate = pattern.asMatchPredicate();

System.out.println(matchPredicate.test("abc")); // true
System.out.println(matchPredicate.test("aaaabc")); // true
System.out.println(matchPredicate.test("bc")); // false

static Pattern compile (String regex, int flags)

Compiles the given regular expression into a pattern with the given flags.

final var pattern = Pattern.compile("abc", Pattern.CASE_INSENSITIVE);
final var matchPredicate = pattern.asMatchPredicate();

System.out.println(matchPredicate.test("abc")); // true
System.out.println(matchPredicate.test("ABC")); // true
final var pattern = Pattern.compile("abc");
final var matchPredicate = pattern.asMatchPredicate();

System.out.println(matchPredicate.test("abc")); // true
System.out.println(matchPredicate.test("ABC")); // false

int flags ()

Returns this pattern's match flags.

final var pattern = Pattern.compile("abc", Pattern.LITERAL);

System.out.printf("%#x%n", Pattern.LITERAL); // 0x10
System.out.printf("%#x%n", pattern.flags()); // 0x10
final var pattern = Pattern.compile("abc",
        Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);

System.out.printf("%#x%n", Pattern.UNICODE_CASE); // 0x40
System.out.printf("%#x%n", Pattern.CASE_INSENSITIVE); // 0x2
System.out.printf("%#x%n", pattern.flags()); // 0x42

Matcher matcher (CharSequence input)

Creates a matcher that will match the given input against this pattern.

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("zyz aaaabc 012");

if (matcher.find()) {
    System.out.println(matcher.group()); // "aaaabc"
}

static boolean matches (String regex, CharSequence input)

Compiles the given regular expression and attempts to match the given input against it.

System.out.println(Pattern.matches("a+bc", "aaabc")); // true
System.out.println(Pattern.matches("a+bc", "aaabc xyz")); // false

System.out.println(Pattern.matches("[a-z]+", "abcxyz")); // true
System.out.println(Pattern.matches("[a-z]+", "0123")); // false

Map<String,Integer> namedGroups ()

Returns an unmodifiable map from capturing group names to group numbers.

final var pattern = Pattern.compile("(?<A>abc|efg)_012_(?<B>x..)");
System.out.println(pattern.namedGroups()); // {A=1, B=2}

String pattern ()

Returns the regular expression from which this pattern was compiled.

final var pattern = Pattern.compile("a+bc");
System.out.println(pattern.pattern()); // "a+bc"
final var pattern = Pattern.compile("[a-z]+");
System.out.println(pattern.pattern()); // "[a-z]+"

static String quote (String s)

Returns a literal pattern String for the specified String.

final var regex = "a+bc";

final var quote = Pattern.quote(regex);
System.out.println(quote); // "\Qa+bc\E"

final var pattern = Pattern.compile(quote);
final var matchPredicate = pattern.asMatchPredicate();

System.out.println(matchPredicate.test("a+bc")); // true
System.out.println(matchPredicate.test("aaabc")); // false

String[] split (CharSequence input)

Splits the given input sequence around matches of this pattern.

final var input = "boo:and:foo";

final var pattern = Pattern.compile(":");
final var ret = pattern.split(input);
System.out.println(Arrays.toString(ret)); // [boo, and, foo]
final var input = "boo:and:foo";
final var pattern = Pattern.compile("o");

final var ret = pattern.split(input);
System.out.println(Arrays.toString(ret)); // [b, , :and:f]

String[] split (CharSequence input, int limit)

Splits the given input sequence around matches of this pattern.

final var input = "boo:and:foo";
final var pattern = Pattern.compile(":");

final var ret1 = pattern.split(input, 2);
System.out.println(Arrays.toString(ret1)); // [boo, and:foo]

final var ret2 = pattern.split(input, 5);
System.out.println(Arrays.toString(ret2)); // [boo, and, foo]

final var ret3 = pattern.split(input, -2);
System.out.println(Arrays.toString(ret3)); // [boo, and, foo]
final var input = "boo:and:foo";
final var pattern = Pattern.compile("o");

final var ret1 = pattern.split(input, 5);
System.out.println(Arrays.toString(ret1)); // [b, , :and:f, , ]

final var ret2 = pattern.split(input, -2);
System.out.println(Arrays.toString(ret2)); // [b, , :and:f, , ]

final var ret3 = pattern.split(input, 0);
System.out.println(Arrays.toString(ret3)); // [b, , :and:f]

Stream<String> splitAsStream (CharSequence input)

Creates a stream from the given input sequence around matches of this pattern.

final var input = "boo:and:foo";
final var pattern = Pattern.compile(":");
final var stream = pattern.splitAsStream(input);

stream.forEach(s -> {
    System.out.println(s);
});

// Result
// ↓
// "boo"
// "and"
// "foo"
final var input = "boo:and:foo";
final var pattern = Pattern.compile("o");
final var stream = pattern.splitAsStream(input);

stream.forEach(s -> {
    System.out.println(s);
});

// Result
// ↓
// "b"
// ""
// ":and:f"

String[] splitWithDelimiters (CharSequence input, int limit)

Splits the given input sequence around matches of this pattern and returns both the strings and the matching delimiters.

final var input = "boo:and:foo";
final var pattern = Pattern.compile(":");

final var ret1 = pattern.splitWithDelimiters(input, 2);
System.out.println(Arrays.toString(ret1)); // [boo, :, and:foo]

final var ret2 = pattern.splitWithDelimiters(input, 5);
System.out.println(Arrays.toString(ret2)); // [boo, :, and, :, foo]

final var ret3 = pattern.splitWithDelimiters(input, -2);
System.out.println(Arrays.toString(ret3)); // [boo, :, and, :, foo]
final var input = "boo:and:foo";
final var pattern = Pattern.compile("o");

final var ret1 = pattern.splitWithDelimiters(input, 5);
System.out.println(Arrays.toString(ret1)); // [b, o, , o, :and:f, o, , o, ]

final var ret2 = pattern.splitWithDelimiters(input, -2);
System.out.println(Arrays.toString(ret2)); // [b, o, , o, :and:f, o, , o, ]

final var ret3 = pattern.splitWithDelimiters(input, 0);
System.out.println(Arrays.toString(ret3)); // [b, o, , o, :and:f, o, , o]

String toString ()

Returns the string representation of this pattern.

final var pattern = Pattern.compile("a+bc");
final var str = pattern.toString();
System.out.println(str); // "a+bc"
final var pattern = Pattern.compile("[a-z]+");
final var str = pattern.toString();
System.out.println(str); // "[a-z]+"

Related posts

To top of page