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

Le caractère spécial \ D des expressions régulières Java

Expression sous-jacente/Le caractère spécial " \ D correspondant aux caractères non numériques.

exemple1

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
   public static void main( String args[] ) {
      String regex = "\\D";
      String input = "Ceci est un texte d'exemple" 12 24 56 89 24";
      Pattern p = Pattern.compile(regex);
      Matcher m = p.matcher(input);
      int count = 0;
      while(m.find()) {
         count++;
      }
      System.out.println("Nombre des correspondances: ")+count);
   }
}

Résultat de la sortie

Nombre de correspondances: 24

exemple2

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 = "\\D";
      Scanner sc = new Scanner(System.in);
      System.out.println("Entrer 5 chaînes d'entrée: ");
      String input[] = new String[5];
      for (int i=0; i<5; i++) {
         input[i] = sc.nextLine();
      }
      //Créer un objet Pattern
      Pattern p = Pattern.compile(regex);
      System.out.println("Nombre de non-caractères numériques dans chaque chaîne: ");
      for(int i=0; i<5;i++) {
         //Créer un objet Matcher
         Matcher m = p.matcher(input[i]);
         int count = 0;
         while(m.find()) {
            count++;
         }
         System.out.println("String "+i+: "+count);
      }
   }
}

Résultat de la sortie

Entrer 5 chaînes d'entrée:
sample 1
12 35 36 63
test 243 663
hello
hello how are you *
Nombre de non-caractères numériques dans chaque chaîne:
String 0: 7
String 1: 3
String 2: 6
String 3: 5
String 4: 19
Vous pourriez aussi aimer