English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Expression sous-jacente/Caractère de métacaractère " re *correspond à 0 ou plusieurs des expressions précédentes.
import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "aabc"*"; String input = "aabcabcaabcabbcaabcbcaabc"; 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 : 4
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatchAllCharacters { public static void main( String args[] ) { String regex = "(.*)?(\\d+)"; Scanner sc = new Scanner(System.in); System.out.println("Entrez 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 chiffres : "); 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
données de test hello how are you 335 Bienvenue à w3codebox Chains contenant des chiffres : sample text 1 hello how are you 335