Computer >> 컴퓨터 >  >> 프로그램 작성 >> Java

예제가 있는 Java의 MatchResult end() 메서드.

<시간/>

java.util.regex.MatcheResult 인터페이스는 일치 결과를 검색하는 메소드를 제공합니다.

toMatchResult()를 사용하여 이 인터페이스의 개체를 가져올 수 있습니다. 매처 메소드 수업. 이 메서드는 현재 matcher의 일치 상태를 나타내는 MatchResult 개체를 반환합니다.

end() 이 인터페이스의 메소드는 마지막 일치가 발생한 후 오프셋을 반환합니다.

import java.util.Scanner;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
   public static void main( String args[] ) {
      String regex = "you$";
      //Reading input from user
      Scanner sc = new Scanner(System.in);
      String input = "Hello how are you";
      //Instantiating the Pattern class
      Pattern pattern = Pattern.compile(regex);
      //Instantiating the Matcher class
      Matcher matcher = pattern.matcher(input);
      //verifying whether a match occurred
      if(matcher.find()) {
         System.out.println("Match found");
      }
      MatchResult res = matcher.toMatchResult();
      int end = res.end();
      System.out.println(end);
   }
}

출력

Enter input text:
hello how are you
Match found
17