How to check multiple regex patterns against an input? Using Java.



In Java, the strings that are used to find the pattern are known as regular expressions. In this article, we will learn to check multiple regex patterns against an input, and it can be done by using 2 approaches.

Using Metacharacter

The meta character "|" in Java regular expressions allows you to match more than one regular expression. For example, if you need to match a particular input text with more than one expression, you need to separate them using the following:

exp1|exp2|exp3

Example

Below is an example to check multiple regex patterns against an input using Metacharacter:

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SampleExample {
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter your input: ");
      String input = sc.nextLine();
      //Regular expression
      String regex = "\d{10}|^[aeiou]";
      //Creating a pattern object
      Pattern pattern = Pattern.compile(regex);
      //Creating a Matcher object
      Matcher matcher = pattern.matcher(input);
      if(matcher.find()) {
         System.out.println(input+" is valid");
      }else {
         System.out.println(input+" is not valid");
      }
   }
}

Output 1

Enter your input:
9848033228
9848033228 is valid

Output 2

Enter your input:
an apple a day keeps doctor away
an apple a day keeps doctor away is valid

Using the List object

Another solution is that you compile all the regular expressions using individual Pattern objects and add them to a list object, and find the match on the input text as ?

List list = new ArrayList<>();
list.add(Pattern.compile(regex1));
list.add(Pattern.compile(regex2));
for (Pattern pattern: list) {
   Matcher matcher = pattern.matcher(input);
   if(matcher.find()) {
      . . . . . . . . . . . . . . .
   }else {
      . . . . . . . . . . . . . . .
   }
}

Example

Below is an example to check multiple regex patterns against an input using the List object:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MultipleRegex {
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter your input: ");
      String input = sc.nextLine();
      //Regular expressions
      String regex1 = "\d{10}";
      String regex2 = "^[aeiou]";
      //Creating a pattern objects
      Pattern pattern1 = Pattern.compile(regex1);
      Pattern pattern2 = Pattern.compile(regex2);
      //Creating an List object
      List<Pattern> patterns = new ArrayList<>();
      patterns.add(pattern1);
      patterns.add(pattern2);
      for (Pattern pattern: patterns) {
         Matcher matcher = pattern.matcher(input);
         if(matcher.find()) {
            System.out.println("For regex "+pattern.pattern()+" "+input+" is valid");
         } else {
            System.out.println("For regex "+pattern.pattern()+" "+input+" is not valid");
         }
      }
   }
}

Output 1

Enter your input:
9848033228
For regex \d{10} 9848033228 is valid
For regex ^[aeiou] 9848033228 is not valid

Output 2

Enter your input:
an apple a day keeps doctor away
For regex \d{10} an apple a day keeps doctor away is not valid
For regex ^[aeiou] an apple a day keeps doctor away is valid
Updated on: 2025-07-15T17:58:40+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements