Java : Pattern (正規表現) - API使用例
Pattern (Java SE 22 & JDK 22) の使い方まとめです。
ほとんどのメソッドにサンプルコードがあります。
API仕様書のおともにどうぞ。
概要
Patternクラスは、コンパイルされた正規表現を表します。
同じ正規表現を何度も使う場合、コンパイル済みの 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("ab")); // false
System.out.println(matchPredicate.test("abcD")); // false
System.out.println(matchPredicate.test("Zabc")); // false
// 部分一致の確認
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
// 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"
}
フィールド
static final int CANON_EQ
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
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
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
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
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
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
// 全角文字です。
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
final var regex = "\\p{Lower}+";
// 半角文字です。
final var input1 = "abc";
// 全角文字です。
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
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
}
メソッド
Predicate<String> asMatchPredicate ()
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 ()
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)
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)
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 ()
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)
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)
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 ()
final var pattern = Pattern.compile("(?<A>abc|efg)_012_(?<B>x..)");
System.out.println(pattern.namedGroups()); // {A=1, B=2}
String pattern ()
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)
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)
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)
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)
final var input = "boo:and:foo";
final var pattern = Pattern.compile(":");
final var stream = pattern.splitAsStream(input);
stream.forEach(s -> {
System.out.println(s);
});
// 結果
// ↓
// "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);
});
// 結果
// ↓
// "b"
// ""
// ":and:f"
String[] splitWithDelimiters (CharSequence input, int limit)
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 ()
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]+"
関連記事
- API 使用例