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

بناء تعبير النمط العادي "[^ ...]" في Java

عبرة فرعية / رمز " [^ ...] يُمثل أي حرف فردي وليس بالدواليب.

مثال 1

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SpecifiedCharacters {
   public static void main(String args[]) {
      String regex = "[^hwtyoupi]";
      String input = "Hi how are you welcome to w3codebox";
      Pattern p = Pattern.compile(regex);
      Matcher m = p.matcher(input);
      int count = 0;
      while(m.find()) {
         count++;
      }
      System.out.println("عدد التطابقات: " + count);
   }
}

نتائج الإخراج

عدد التطابقات: 21

مثال 2

برنامج Java التالي يقبل خمس كلمات من المستخدم ويطبع الكلمات / العبارات التي لا تحتوي على أحرف إنجليزية.

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
   public static void main(String args[]) {
      String regex = "^.*[^a-zA-Z].*$";
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter 5 input strings:");
      String input[] = new String[5];
      for(int i=0; i<5; i++) {
         input[i] = sc.nextLine();
      }
      // Create a Pattern object
      Pattern p = Pattern.compile(regex);
      System.out.println("Strings that do not contain English alphabet:");
      for(int i=0; i<5; i++) {
         // Create a Matcher object
         Matcher m = p.matcher(input[i]);
         if(m.find()) {
            System.out.println(m.group());
         }
      }
   }
}

نتائج الإخراج

Enter 5 input strings:
1234*5
*&%
sample
test
data23
Strings that do not contain English alphabet:
1234*5
*&%
قد تعجبك