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

Program to match vowels in a string using Java regular expressions

[] ” within the match/Subexpression “ [] Matches all specified characters. Therefore, to match all letters, specify the vowels as shown below-

[aeiouAEIOU]

Example1

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatchVowels {
   public static void main(String args[]) {
      String regex = "[aeiouAEIOU]";
      System.out.println("Enter input string:");
      Scanner sc = new Scanner(System.in);
      String input = sc.nextLine();
      //Compile Regular Expression
      Pattern.compile(regex);
      //Compile Regular Expression
      Pattern pattern = Pattern.compile(regex);
      Matcher matcher = pattern.matcher(input);
      if(matcher.find()) {
         System.out.println("The input string contains vowels");
      } else {
         System.out.println("The input string does not contain vowels");
      }
   }
}

Output Result

Enter input string:
hello how are you welcome
The input string contains vowels

Example2

import java.util.Scanner;
public class Test {
   public static void main(String args[]) {
      String regex = "[aeiouAEIOU]";
      System.out.println("Enter input string:");
      Scanner sc = new Scanner(System.in);
      String input = sc.nextLine();
      boolean result = input.matches(regex);
      if(result) {
         System.out.println("The input string contains vowels");
      } else {
         System.out.println("The input string does not contain vowels");
      }
   }
}

Output Result

Enter input string:
hello how are you welcome
The input string does not contain vowels