English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
يتحقق طريقة Match() لنص Java من تطابق النص مع تعبير النمط المقدم
جملة syntax للطريقة matches() للنص
string.matches(String regex)
في هذا السياق،string هو عنصر من فئة String.
regex - تعبير النمط
إذا تطابق تعبير النمط مع النصيعود بـtrue
إذا لم تطابق تعبير النمط مع النصيعود بـfalse
class Main { public static void main(String[] args) { //نمط تعبير正则 //بداية بسلسلة الحروف 'a' وتنتهي بسلسلة الحروف '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 } }
هنا "^a...s$" هو تعبير نمطي، يمثل النص s الذي يبدأ بـ a وينتهي بـ s من خمس أحرف.
// التحقق من أن النص يحتوي فقط على أرقام class Main { public static void main(String[] args) { // نمط البحث المخصص للأرقام فقط 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 } }
هنا "^[0-9]+$" هو تعبير نمطي، يمثل فقط الأرقام.