English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

كيفية استخدام Java RegEx لتمثيل الألفاظ غير الألفاظية؟

كل الأحرف غير الأبجدية (كاملة من الأحرف الكبيرة والصغيرة) والرقم (من 0 إلى 9) تُعتبر حروف غير كلمات. يمكنك استخدام الحرف الخاص "\ W" لتمثيلها.

مثال1

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");
      }
   }
}

الخروج 1

ادخل String
*&&^#
Match occurred

الخروج 2

ادخل String
hello
Match not occurred

مثال2

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
من المحتمل أن تعجبك