English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Simple character class " [] "Matches all specified characters. Meta-character"^It is used as a negation symbol in the above character class, meaning the following expression matches all characters except b (including spaces and special characters).
"[^b]"
Similarly, the following expression matches all consonants in the given input string.
"([^aeiouyAEIOUY0-9\\W]+);
Then, you can use the replaceAll() method to remove the matched characters by replacing them with an empty string "".
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:\n");+result); } }
Output result
Result: i e#oe o $uoi$aoi
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 string:\n"); String input = sc.nextLine(); String regex = "([^aeiouyAEIOUY0-9\W])"; String constants = ""; /Create a pattern object Pattern pattern = Pattern.compile(regex); //Match the compiled pattern in the string Matcher matcher = pattern.matcher(input); //Create 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 result
Enter string: # Hello how are you welcome to ooo # Result: # eo o ae you eoe o ooo #