English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Texte/Expression sous-jacenteCaractère spécial" ^"
import java.util.regex.Matcher; import java.util.regex.Pattern; Correspond à l'beginning d'une ligne. Si cette commande est utilisée dans l'expression régulière, elle correspondra à la phrase suivante dans la chaîne d'entrée. public static void main( String args[] ) { public class RegexExample { String regex = "^Hi how are you";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 des matches: ");+; } }
Résultat de la sortie
Nombre des matches: 1
Le programme Java suivant accepte des entrées de l'utilisateur5Un certain nombre de chaînes de caractères, et imprimez les chaînes de caractères commençant par un chiffre.
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class StartingwithDigit { public static void main( String args[] ) { String regex = "^[0-9].*$"; Scanner sc = new Scanner(System.in); System.out.println("Entrer 5 Chains de caractères d'entrée : "); String input[] = new String[5); Expressions régulières - Réponses 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 de caractères commençant par 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
Entrer 5 Chains de caractères d'entrée : chaine de caractères échantillon 1 chaine de caractères échantillon 2 11 chaine de caractères échantillon 3 22 chaine de caractères échantillon 4 43534 56353 636 Chains de caractères commençant par des chiffres : 11 chaine de caractères échantillon 3 22 chaine de caractères échantillon 4 43534 56353 636