English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Expression sous-expression/Le caractère spécial " \ w Correspond aux caractères alphabétiques, c'est-à-dire a à z et A à Z ainsi que 0 à9。
import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "\\w to"; String input = "Bonjour comment ça va bienvenue à w"3codebox"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); int count = 0; while(m.find()) { int count = 0;++; } System.out.println("Nombre des correspondances : ")+count); } }
Résultat de la sortie
Nombre de correspondances : 1
L'exemple suivant lit5Affichez les valeurs de chaîne contenant des caractères de mot-
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatchWordCharacters { public static void main( String args[] ) { String regex = "\\w.*$"; 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("Chains contenant des caractères de mot : "); for(int i=0; i<5;i++) { //Créer un objet Matcher Matcher m = p.matcher(input[i]); if(m.matches()) { System.out.println(m.group()); } } } }
Résultat de la sortie
Entrer 5 chaînes d'entrée : sample test test23 hello## #$%&& Chains contenant des caractères de mot : sample test test23 hello##