English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Classe de caractères simple " [] Correspond à tous les caractères spécifiés. Caractère spécial^Dans le caractère de la classe ci-dessus, il est utilisé comme négateur, c'est-à-dire que l'expression suivante correspond à tous les caractères sauf b (y compris les espaces et les caractères spéciaux).
"[^b]"
De même, l'expression suivante correspond à tous les consonnes de la chaîne d'entrée donnée.
"([^aeiouyAEIOUY0-9\\W]+)";
Ensuite, vous pouvez utiliser la méthode replaceAll() pour supprimer les caractères correspondants en les remplaçant par une chaîne vide "".
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("Résultat: ")}+result); } }
Résultat de sortie
Résultat: 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("Entrez une chaîne de caractères: "); String input = sc.nextLine(); String regex = "([^aeiouyAEIOUY0-9\\W])"; String constants = ""; /Créer un objet modèle Pattern pattern = Pattern.compile(regex); //Contraîre les modèles de chaîne dans la chaîne Matcher matcher = pattern.matcher(input); //Créer un tampon de chaîne vide StringBuffer sb = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb, ""); } matcher.appendTail(sb); System.out.println("Résultat: \n");+ sb.toString()); } }
Résultat de sortie
Entrez une chaîne de caractères: # Hello how are you welcome to ooo # Résultat: # eo o ae you eoe o ooo #