English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
La méthode Match() de String Match() vérifie si la chaîne correspond à l'expression régulière donnée en Java.
La syntaxe de la méthode matches() de la chaîne est :
string.matches(String regex)
Ici, string est un objet de la classe String.
regex - Expression régulière
Si l'expression régulière correspond à la chaîneRetourne true
Si l'expression régulière ne correspond pas à la chaîneRetourne false
class Main { public static void main(String[] args) { //Modèle d'expression régulière //Une chaîne de cinq lettres commençant par 'a' et se terminant par 's' String regex = "^a...s$"; System.out.println("abs".matches(regex)); // false System.out.println("alias".matches(regex)); // true System.out.println("an abacus".matches(regex)); // false System.out.println("abyss".matches(regex)); // true } }
Here "^a...s$" is a regular expression, indicating a string starting with a and ending with s5string s with a certain number of letters.
//Check if the string contains only numbers class Main { public static void main(String[] args) { //Pattern to search only for numbers String regex = "^[0-9]+$"; System.out.println("123a".matches(regex)); // false System.out.println("98416".matches(regex)); // true System.out.println("98 41".matches(regex)); // false } }
Here "^[0-9]+$" is a regular expression, which only represents numbers.