LoginSignup
7
6

More than 5 years have passed since last update.

Java:文字列のパターンマッチング

Last updated at Posted at 2015-02-17

参考

素晴らしいサイト

記述例

Main.java
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    static final String text = "Sample Text hoge Hellow World piyo and hoge Good-bye World piyo";

    public static void main(String args[]) throws Exception {

        Pattern p1 = Pattern.compile("hoge");
        Pattern p2 = Pattern.compile("piyo");

        Matcher m1 = p1.matcher(text);
        Matcher m2 = p2.matcher(text);

        while (m1.find() && m2.find()) {
            System.out.println(text.substring(m1.end(), m2.start()));
        }
    }
}

 Hellow World 
 Good-bye World 
7
6
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
7
6