English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Les caractères simples "[]" correspondent à tous les caractères spécifiés. L'expression suivante correspond aux caractères autres que xyz.
"[xyz]"
De même, l'expression suivante correspond à toutes les voyelles dans la chaîne d'entrée donnée.
"([^aeiouAEIOU0-9\\W]+)";
Ensuite, vous pouvez utiliser une chaîne vide "" pour remplacer les caractères correspondants, en utilisant la méthode replaceAll().
public class RemovingVowels { public static void main( String args[] ) { String input = "Hi welcome to w3codebox"; String regex = "[aeiouAEIOU]"; String result = input.replaceAll(regex, ""); System.out.println("Résultat : "+result); } }Résultat de la sortie
Résultat : H wlcm t ttrlspnt
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main( String args[] ) { Scanner sc = new Scanner(System.in); System.out.println("Entrer une chaîne de caractères :"); String input = sc.nextLine(); String regex = "[aeiouAEIOU]"; String constants = ""; System.out.println("Chaîne d'entrée: \n"+input); //Créer un objet modèle Pattern pattern = Pattern.compile(regex); //Concordance du modèle de chaîne Matcher matcher = pattern.matcher(input); //Créer un tampon de chaîne vide StringBuffer sb = new StringBuffer(); while (matcher.find()) { constants = constants+matcher.group(); matcher.appendReplacement(sb, ""); } matcher.appendTail(sb); System.out.println("Résultat: \n"+ sb.toString()+constants ); } }Résultat de la sortie
Entrer une chaîne de caractères : this is a sample text Chaîne d'entrée: this is a sample text Résultat: ths s smpl txtiiaaee