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

Beispiel für die Methode toString() im Java Matcher

dieserjava.util.regex.MatcherKlasse repräsentiert einen Motor, der verschiedene Matching-Operationen durchführt. Diese Klasse hat keinen Konstruktor, und sie kannmatches()Methoden der Klasse java.util.regex.Pattern erstellen/Erhält ein Objekt dieser Klasse.

Klasse MatchertoString()Die Methode gibt einen String-Wert zurück, der den Inhalt des aktuellen Matcher-Objekts darstellt.

Beispiel1

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ToStringExample {
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Geben Sie den Eingabeplaintext ein: ");
      String input = sc.nextLine();
      String regex = "[#%&*]";
      //Erstellen Sie ein Musterobjekt
      Pattern pattern = Pattern.compile(regex);
      //Create a Matcher object
      Matcher matcher = pattern.matcher(input);
      int count =0;
      while(matcher.find()) {
         count++;
      }
      //Search pattern used
      System.out.println("The are "+count+" special [# % & *] characters in the given text");
      System.out.println("Following is the string format of the matcher used: \n"+matcher.toString());
   }
}

Output result

Enter input text:
Hello# How # are # you *& welcome to T#utorials%point
The are 7 special [# % & *] characters in the given text
Following is the string format of the matcher used:
java.util.regex.Matcher[pattern=[#%&*] region=0,52 lastmatch=]

Beispiel2

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ToStringExample {
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Geben Sie den Eingabeplaintext ein: ");
      String input = sc.nextLine();
      String regex = "[#%&*]";
      //Erstellen Sie ein Musterobjekt
      Pattern pattern = Pattern.compile(regex);
      //Create a Matcher object
      Matcher matcher = pattern.matcher(input);
      int count =0;
      while(matcher.find()) {
         count++;
      }
      //Search pattern used
      System.out.println("The are "+count+" special [# % & *] characters in the given text");
      System.out.println("Following is the string format of the matcher used: \n"+matcher.toString());
   }
}

Output result

Enter input text:
Hello# How # are # you *& welcome to T#utorials%point
The are 7 special [# % & *] characters in the given text
Following is the string format of the matcher used:
java.util.regex.Matcher[pattern=[#%&*] region=0,52 lastmatch=]