English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Voici l'expression régulière pour correspondre aux lettres dans l'entrée donnée-
"^[a-zA-Z]*$"
où,
^ correspond au début de la phrase.
[a-zA-[a] correspond aux lettres minuscules et majuscules.
*représente zéro ou plusieurs fois.
& représente la fin de la ligne.
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ContainsAlphabetExample { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String names[] = new String[5; for (int i = 0; i < names.length; i++} System.out.println("Entrez votre nom: "); names[i] = sc.nextLine(); } //Expression régulière acceptant les lettres anglaises String regex = "^[a-zA-Z]*$"; //Créer un objet modèle Pattern pattern = Pattern.compile(regex); for (String name : names) { //Créer un objet Matcher Matcher matcher = pattern.matcher(name); if(matcher.matches()) {}} System.out.println(name+" est un nom valide"); } else { System.out.println(name+" n'est pas un nom valide"); } } } }
Résultat de la sortie
Entrez votre nom: krishna Entrez votre nom: kasyap Entrez votre nom: maruthi# Entrez votre nom: Sai_Ram Entrez votre nom: Vani.Viswanath krishna est un nom valide kasyap est un nom valide maruthi# n'est pas un nom valide Sai_Ram n'est pas un nom valide Vani.Viswanath n'est pas un nom valide
import java.util.Scanner; public class Just { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Entrez votre nom: "); String name = sc.nextLine(); String regex = "^[a-zA-Z]*$"; boolean result = name.matches(regex); if(result) { System.out.println("Le nom donné est valide"); } else { System.out.println("Le nom donné n'est pas valide"); } } }
Résultat de la sortie
Entrez votre nom: vasu#dev Le nom donné n'est pas valide