 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to match vowels in a string using regular expression in Java
You can group all the required characters to match within the square braces “[ ]” i.e. The metacharacter/sub-expression “[ ]” matches all the specified characters. Therefore, to match all the letters specify the vowel letters within these as shown below −
[aeiouAEIOU]
Example 1
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();       //Compiling the regular expression       Pattern.compile(regex);       //Compiling the 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
Enter input string: hello how are you welcome The input string contains vowels
Example 2
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
Enter input string: hello how are you welcome The input string does not contain vowels
Advertisements
 