English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
java.util.regex.Matcher يُمثل محركًا يُجري جميع العمليات المُطابقة. لا يحتوي على مُحرك بناء، يمكن استخدامهmatches()
يُنشئ أو يحصل على موضوع من طريقة Pattern في java.util.regex.Pattern.،
مفهوم MatcherusePattern()يستقبل الطريقة نموذجًا يمثل نموذج النص الاصطناعي الجديد ويستخدمه للبحث عن التطابق.
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class UsePatternExample { public static void main(string[] args) { scanner sc = new scanner(system.in); System.out.println("أدخل النص الإدخالي:"); String input = sc.nextLine(); String regex = "[#%&*]"; //إنشاء نموذج Pattern pattern = Pattern.compile(regex); //إنشاءMatcher Matcher matcher = pattern.matcher(input); int count = 0; while(matcher.find()) { count++; } //البحث باستخدام النمط System.out.println("هناك " + count + " رمز خاص [ # % & * ] في النص المقدم"); //إنشاء نموذج يقبل 5 إلى 6 حروف Pattern newPattern = Pattern.compile("\\A(?=\\w{6,15}\\z)"); //الانتقال إلى نموذج جديد matcher = matcher.usePattern(newPattern); if(matcher.find()) { System.out.println("الإدخال المقدم يحتوي على 6 إلى 15 حرف"); } else { System.out.println("الإدخال المقدم لا يحتوي على 6 إلى 15 حرف"); } } }
نتيجة الخروج
أدخل النص الإدخالي: #*mypassword& هناك 3 رموز خاصة [ # % & * ] في النص المقدم !!mypassword! الإدخال المقدم لا يحتوي على 6 إلى 15 حرف