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

Erklärung des Metazeichens „\ B“ in Java-Regulär Ausdrücken (RegEx).

Teilausdruck/Metasymbole " \ B mit dem Nicht-Wortgrenzbereich übereinstimmen.

Beispiel1

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("Geben Sie einen String ein: ");
      String input = sc.nextLine();
      Pattern p = Pattern.compile(regex);
      Matcher m = p.matcher(input);
      int count = 0;
      while(m.find()) {
         count ++;
      }
      System.out.println("Number of matches: "+count);
   }
}

输出结果

Enter a string:
A sentence doesn't end with because because, because is a conjunction
Number of matches: 3

Beispiel2

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

输出结果

Enter input string:
Hello how are you welcome to w3codebox
Non-word boundaries:
30