English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
كل الأحرف غير الأبجدية (كاملة من الأحرف الكبيرة والصغيرة) والرقم (من 0 إلى 9) تُعتبر حروف غير كلمات. يمكنك استخدام الحرف الخاص "\ W" لتمثيلها.
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String args[]) { // من خلال المستخدم قراءة String System.out.println("ادخل String"); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); String regex = "^\\W{5}"; //编译正则表达式 Pattern pattern = Pattern.compile(regex); //检索匹配器对象 Matcher matcher = pattern.matcher(input); if(matcher.find()) { System.out.println("Match occurred"); } else { System.out.println("Match not occurred"); } } }
ادخل String *&&^# Match occurred
ادخل String hello Match not occurred
import java.util.Scanner; public class RegexExample { public static void main( String args[] ) { String regex = "\\W*"; System.out.println("ادخل قيمة الدخول:"); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); boolean bool = input.matches(regex); if(bool) { System.out.println("match occurred"); } else { System.out.println("match not occurred"); } } }
نتيجة الخروج
ادخل قيمة الدخول: #*** occurred match