English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
expression sous-jacente/caractère de syntaxe " re {n, m} au moins n et au plus m correspondances avec l'expression précédente.
import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main(String args[]) { String regex = "xyy{2,4"; String input = "xxyyzxxyyyyxyyzxxyyzz"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); int count = 0; while(m.find()) { count++; } System.out.println("Nombre des correspondances: ")+count); } }
Résultat de la sortie
Nombre de correspondances: 1
Le programme Java suivant lit la valeur du nom de l'utilisateur et autorise uniquement1à20 caractères.
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main(String args[] ) { //Expression régulière pour correspondre aux caractères au moins 1 almost 20 String regex = "[a-zA-Z]{1,20"; Scanner sc = new Scanner(System.in); System.out.println("Entrez le nom de l'étudiant:"); String name = sc.nextLine(); Pattern p = Pattern.compile(regex); Matcher m = p.matcher(name); if(m.matches()) { System.out.println("Le nom est approprié"); } else { System.out.println("Le nom est inapproprié"); } } }
Entrez le nom de l'étudiant: Mouktika Le nom est approprié
Entrez le nom de l'étudiant: ka 34 Le nom est inapproprié
Entrez le nom de l'étudiant: Sri Veera Venkata Satya Sai Suresh Santosh Samrat Le nom est inapproprié