Java Regular Expression find

Introduction

We can use find() to determine if the input sequence contains a subsequence that matches the pattern.


// Use find() to find a subsequence. 
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
  public static void main(String args[]) {
    Pattern pat = Pattern.compile("Java");
    Matcher mat = pat.matcher("Java 8");

    System.out.println("Looking for Java in Java 8.");

    if (mat.find())
      System.out.println("subsequence found");
    else//from   w  ww  . j  a  v a2s  .  com
      System.out.println("No Match");
  }
}



PreviousNext

Related