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

Wie man mit Java RegEx nicht-numerische Zeichen matcht?

Sie können den Metazeichen " \\ DPassen Sie nicht-numerische Zeichen

Example1

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
   public static void main(String args[]) {
      //Lesen Sie einen String vom Benutzer
      System.out.println("Geben Sie einen String ein");
      Scanner sc = new Scanner(System.in);
      String input = sc.nextLine();
      String regex = "\\D";
      //Compile regular expression
      Pattern pattern = Pattern.compile(regex);
      //Search matcher object
      Matcher matcher = pattern.matcher(input);
      int count = 0;
      while(matcher.find()) {
         count++;
      }
      System.out.println("Number non-digit characters: "+count);
   }
}

Output result

Enter a String
sample text 2425 36
Number non-digit characters: 13

Example2

import java.util.Scanner;
public class RegexExample {
   public static void main(String args[]) {
      //Regular expressions accept5words with letters
      String regex = "\\D{10};
      System.out.println("Enter input value:");
      Scanner sc = new Scanner(System.in);
      String input = sc.nextLine();
      boolean result = input.matches(regex);
      if(result) {
         System.out.println("input matched");
      }
      else {
         System.out.println("wrong input");
      }
   }
}

Output1

Enter input value:
sample abc
input matched

Output2

Enter input value:
sample1234
wrong input