English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

find() method in Java with examples

java.util.regex.Matcher class represents an engine for performing various matching operations. This class has no constructor and can be used withmatches()methods created by the java.util.regex.Pattern class/to obtain an object of this class.

of this kindfind()The method attempts to find the next subsequent input that matches the current Matcher object, and if a match is found, this method returns true, otherwise it returns false.

Example

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FindExample {
   public static void main( String args[] ) {
      //Read string value
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter input string");
      String input = sc.nextLine();
      //Regular expression to find digits
      String regex = "(\\D)";
      //Compile the regular expression
      Pattern pattern = Pattern.compile(regex);
      //Retrieve the matcher object
      Matcher matcher = pattern.matcher(input);
      //Verify whether a match occurs
      if(matcher.find()) {
         System.out.println("Given string contain non-digit characters");
      } else {
         System.out.println("Given string does not contain non-digit characters");
      }
   }
}

Output result

Enter input string
11245#
Given string contain non-digit characters