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

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

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

이 클래스(Matcher)의regionEnd()이 메서드는 정수 값을 반환하며, 이 값은 현재 매치어 객체의 종료 인덱스를 나타냅니다.

예제1

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegionEndExample {
   public static void main(String[] args) {
      String regex = "(.*)\\d+)",*)";
      String input = "This is a sample Text, 1234, between numbers.";
      //모델 객체를 생성
      Pattern pattern = Pattern.compile(regex);
      //Matcher 객체를 생성
      Matcher matcher = pattern.matcher(input);
      //매치어의 지역을 설정
      matcher.region(5, 20);
      if(matcher.matches()) {
         System.out.println("일치함");
      } else {
         System.out.println("일치하지 않음");
      }
      System.out.print("지역의 끝: ",+matcher.regionEnd());
   }
}

출력 결과

일치하지 않음
지역의 끝: 20

예제2

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegionEndExample {
   public static void main(String[] args) {
      //정규 표현식은 수락할 수 있습니다6부터10개 문자
      String regex = "[#]";
      System.out.println("Enter a string:");
      Scanner sc = new Scanner(System.in);
      String input = sc.nextLine();
      //모델 객체를 생성
      Pattern pattern = Pattern.compile(regex);
      //Matcher 객체를 생성
      Matcher matcher = pattern.matcher(input);
      //지역을 입력 문자열로 설정
      matcher.region(2, 4);
      //투명 범위로 전환
      if(matcher.find()) {
         System.out.println("일치함");
      } else {
         System.out.println("일치하지 않음");
      }
      System.out.println("지역의 끝: " );+ matcher.regionEnd());
   }
}

출력 결과

문자열을 입력하세요:
이는 샘플 텍스트 #
일치하지 않음
지역의 끝: 4
당신이 좋아할 만한 것