Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

腐れ正規表現

String#matches(String regex) では、単純な部分文字列のマッチングができない。Perl を使っていた身としては窮屈で仕方がない。
以下のコードでは "ONLY WITH .*" が出力される。

public class Hoge {
  public static void main(String[] args) {
    if (("FooBarBaz").matches("Bar")) {
      System.out.println("MATCH");
    } else if (("FooBarBaz").matches(".*Bar.*")) {
      System.out.println("ONLY WITH .*");
    } else {
      System.out.println("DON'T MATCH");
    }
  }
}