
- 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
How to remove consonants from a string using regular expressions in Java?
The simple character class “[ ]” matches all the specified characters in it. The meta character ^ acts as negation within the above character class i.e. the following expression matches all the characters except b (including spaces and special characters)
"[^b]"
Similarly, the following expression matches all the consonants in the given input string.
"([^aeiouyAEIOUY0-9\W]+)";
Then you can remove the matched characters by replacing them with the empty string “”, using the replaceAll() method.
Example 1
public class RemovingConstants { public static void main( String args[] ) { String input = "Hi welc#ome to t$utori$alspoint"; String regex = "([^aeiouAEIOU0-9\W]+)"; String result = input.replaceAll(regex, ""); System.out.println("Result: "+result); } }
Output
Result: i e#oe o $uoi$aoi
Example 2
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RemovingConsonants { public static void main( String args[] ) { Scanner sc = new Scanner(System.in); System.out.println("Enter input string: "); String input = sc.nextLine(); String regex = "([^aeiouyAEIOUY0-9\W])"; String constants = ""; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(input); //Creating an empty string buffer StringBuffer sb = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb, ""); } matcher.appendTail(sb); System.out.println("Result: \n"+ sb.toString() ); } }
Output
Enter input string: # Hello how are you welcome to Tutorialspoint # Result: # eo o ae you eoe o uoiaoi #
- Related Articles
- How to remove vowels from a string using regular expressions in Java?
- Remove Leading Zeroes from a String in Java using regular expressions
- How to extract numbers from a string using regular expressions?
- How to split a string using regular expressions in C#?
- How to find consonants in a given string using Java?
- How to extract data from a string with Python Regular Expressions?
- Return a specific MySQL string using regular expressions
- How to delete consonants from a string in Python?
- How to extract each (English) word from a string using regular expression in Java?
- Date validation using Java Regular Expressions
- Name validation using Java Regular Expressions
- How to validate an email address using Java regular expressions.
- Replace one string with another string with Java Regular Expressions
- How to Split String in Java using Regular Expression?
- Phone Number validation using Java Regular Expressions

Advertisements