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

Java의 Matcher reset() 메서드와 예제

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

에서reset()이(매칭자) 클래스의 메서드는 모든 상태 정보를 제거하고 문자열을 기본 값으로 재설정하여 추가 위치를 0으로 초기화합니다.

예1

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Reset {
   public static void main(String[] args) {
      String str = "<p>This<b>is</b>예제<b>는/b>HTML<b>스크립트<b>는/b>모든<b>를/b>대체 단어<b>는/b>bold<b>는/b></p>.";
      //정규 표현식 매칭을 통해 두서머니 태그의 내용을 매칭합니다
      String regex = "<b>(\\S+/b>";
      //패턴 객체를 생성합니다
      Pattern pattern = Pattern.compile(regex);
      //문자열에서 컴파일된 패턴을 매칭합니다
      Matcher matcher = pattern.matcher(str);
      while (matcher.find()) {
         System.out.println("상태 매칭자: ")+matcher.toMatchResult());
         String result = matcher.group()1);
      }
      matcher = matcher.reset();
      System.out.println("State of the matcher after resetting it: "+matcher.toMatchResult());
   }
}

출력 결과

State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+/b> region=0,116 lastmatch=<b>is</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+/b> region=0,116 lastmatch=<b>example</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+/b> region=0,116 lastmatch=<b>script</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+/b> region=0,116 lastmatch=<b>every</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+/b> region=0,116 lastmatch=<b>>단어</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+/b> region=0,116 lastmatch=<b>bold</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+/b> region=0,116 lastmatch=]
State of the matcher after resetting it: java.util.regex.Matcher[pattern=<b>(\S+/b> region=0,116 lastmatch=]

이 메서드의 또 다른 버전은 문자 데이터를 받아 이를 사용하여 매칭자를 초기화합니다.

예제2 

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Reset {
   public static void main(String[] args) {
      String str = "<p>This<b>is</b>예제<b>는/b>HTML<b>스크립트<b>는/b>모든<b>를/b>대체 단어<b>는/b>bold<b>는/b></p>.";
      //정규 표현식 매칭을 통해 두서머니 태그의 내용을 매칭합니다
      String regex = "(\\S+)";
      //패턴 객체를 생성합니다
      Pattern pattern = Pattern.compile(regex);
      //문자열에서 컴파일된 패턴을 매칭합니다
      Matcher matcher = pattern.matcher(str);
      while (matcher.find()) {
         System.out.println("상태 매칭자: ")+matcher.toMatchResult());
         String result = matcher.group()1);
      }
      matcher = matcher.reset("<b>this</b> is <b>new</b> string <b>after</b> reset");
      while (matcher.find()) {
         System.out.println("State of the matcher after resetting it: "+matcher.toMatchResult());
      }
   }
}

출력 결과

State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+/b> region=0,116 lastmatch=<b>is</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+/b> region=0,116 lastmatch=<b>example</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+/b> region=0,116 lastmatch=<b>script</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+/b> region=0,116 lastmatch=<b>every</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+/b> region=0,116 lastmatch=<b>word</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+/b> region=0,116 lastmatch=<b>bold</b>]
State of the matcher after resetting it: java.util.regex.Matcher[pattern=<b>(\S+/b> region=0,51 lastmatch=<b>this</b>]
State of the matcher after resetting it: java.util.regex.Matcher[pattern=<b>(\S+/b> region=0,51 lastmatch=<b>new</b>]
State of the matcher after resetting it: java.util.regex.Matcher[pattern=<b>(\S+/b> region=0,51 lastmatch=<b>after</b>]