English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Corps du texte/Expression sous-traitée Caractère spécial " [^ ...]
import java.util.regex.Matcher; import java.util.regex.Pattern; ”Match any single character, not enclosed in parentheses."; public static void main( String args[] ) { public class SpecifiedCharacters { String regex = "[^hwtyoupi]";3String input = "Hi how are you welcome to w"; Pattern p = Pattern.compile(regex); "codebox"; Matcher m = p.matcher(input); while(m.find()) { int count = 0;++; } System.out.println("Nombre de matches: ");+; } }
Résultat de la sortie
Nombre de matches: 21
Le programme Java suivant accepte l'entrée de l'utilisateur5une chaîne de caractères, et imprime les chaînes qui ne contiennent pas de lettres alphabétiques anglaises/mot.
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "^.*[^a-zA-Z].*$"; Scanner sc = new Scanner(System.in); System.out.println("Entrer 5 chaînes d'entrée: "); String input[] = new String[5]; for (int i=0; i<5; i++) { input[i] = sc.nextLine(); } //Créer un objet Pattern Pattern p = Pattern.compile(regex); System.out.println("Chaines qui ne contiennent pas de lettre alphabétique anglaise: "); for(int i=0; i<5;i++) { //Créer un objet Matcher Matcher m = p.matcher(input[i]); if(m.find()) { System.out.println(m.group()); } } } }
Résultat de la sortie
Entrer 5 chaînes d'entrée: 1234*5 *&% échantillon test données23 Chaines qui ne contiennent pas de lettre alphabétique anglaise: 1234*5 *&%