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

Java에서 Matcher toString() 메서드 예제

java.util.regex.Matcher에서이 클래스는 매칭 작업을 수행하는 엔진을 대표합니다. 이 클래스는 생성자가 없으며, java.util.regex.Matcher에서 사용할 수 있습니다.matches()java.util.regex.Pattern 메서드를 생성한/이 클래스의 객체를 얻습니다.

Matcher 클래스의toString()메서드는 현재 매치어 객체의 내용을 나타내는 문자열 값을 반환합니다.

예제1

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("Enter input text: ");
      String input = sc.nextLine();
      String regex = "[#%&*]";
      //모델 객체를 생성합니다
      Pattern pattern = Pattern.compile(regex);
      //Matcher 객체 생성
      Matcher matcher = pattern.matcher(input);
      int count =0;
      while(matcher.find()) {
         count++;
      }
      //사용된 패턴 검색
      System.out.println("The are "+count+" special [# % & *] characters in the given text");
      System.out.println("이하는 사용된 매치어의 문자 형식입니다: \n"+matcher.toString());
   }
}

출력 결과

입력 텍스트를 입력하세요:
Hello# How # are# you *& welcome to T#utorials%point
The are 7 special [# % & *] characters in the given text
이하는 사용된 매치어의 문자 형식입니다:
java.util.regex.Matcher[pattern=[#%&*] region=0,52 lastmatch=]

예제2

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("Enter input text: ");
      String input = sc.nextLine();
      String regex = "[#%&*]";
      //모델 객체를 생성합니다
      Pattern pattern = Pattern.compile(regex);
      //Matcher 객체 생성
      Matcher matcher = pattern.matcher(input);
      int count =0;
      while(matcher.find()) {
         count++;
      }
      //사용된 패턴 검색
      System.out.println("The are "+count+" special [# % & *] characters in the given text");
      System.out.println("이하는 사용된 매치어의 문자 형식입니다: \n"+matcher.toString());
   }
}

출력 결과

입력 텍스트를 입력하세요:
Hello# How # are# you *& welcome to T#utorials%point
The are 7 special [# % & *] characters in the given text
이하는 사용된 매치어의 문자 형식입니다:
java.util.regex.Matcher[pattern=[#%&*] region=0,52 lastmatch=]