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

Explication du caractère de métacaractère "\ B" dans l'expression régulière Java.

Expression sous/Caractère de meta- \ B Correspondance avec les limites non-words.

Exemple1

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 = \\\\Bcause;
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a string: ");
      String input = sc.nextLine();
      Pattern p = Pattern.compile(regex);
      Matcher m = p.matcher(input);
      int count = 0;
      while(m.find()) {
         count ++;
      }
      System.out.println("Nombre de correspondances: "+count);
   }
}

Résultat de la sortie

Entrez une chaîne:
Une phrase ne se termine pas par parce que parce que, parce que c'est une conjonction
Nombre de correspondances: 3

Exemple2

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
   public static void main( String args[] ) {
      System.out.println("Enter input string: ");
      Scanner sc = new Scanner(System.in);
      String input = sc.nextLine();
      String regex = "\\B";
      //Compiling the regular expression
      Pattern pattern = Pattern.compile(regex);
      //Retrieving the matcher object
      Matcher matcher = pattern.matcher(input);
      int count =0;
      System.out.println("Non-word boundaries: ");
      while(matcher.find()) {
         count ++;
      }
      System.out.println(count);
   }
}

Résultat de la sortie

Enter input string:
Hello how are you welcome to w3codebox
Non-boundaries: 
30
Vous pourriez aussi aimer