0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

正規表現(マッチ、抽出)

Posted at

自分メモ。

final String  regex = "^(ねこ|いぬ)は(唐揚げ|魚)好き$";
final String text = "ねこは唐揚げが好き";
if (text.matches(regex)) {
    final Pattern p = Pattern.compile(regex);
    if (m.find()) {
        m.group(); // ねこは唐揚げが好き
        m.group(0); // ねこは唐揚げが好き
        m.group(1); // ねこ
        m.group(2); // 唐揚げ 
    }
}

or条件で空文字判定をする場合は(|唐揚げ|魚)とすればよい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?