
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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
- Related Articles
- Java Program to split a string using Regular Expression
- How to match digits using Java Regular Expression (RegEx)
- How to remove vowels from a string using regular expressions in Java?
- How to match a regular expression against a string?
- How to Split String in Java using Regular Expression?
- How to match non-digits using Java Regular Expression (RegEx)
- How to match a word in python using Regular Expression?
- How to match a whitespace in python using Regular Expression?
- How to match at the beginning of string in python using Regular Expression?
- How to match bold fields in a HTML script using a regular expression in Java?
- PHP – Match regular expression using mb_ereg_match()
- How to use regular expression in Java to pattern match?
- How to match a nonwhitespace character in python using Regular Expression?
- How to match a single character in python using Regular Expression?
- How to match only digits in Python using Regular Expression?

Advertisements