English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
正文사용할 수 있습니다클래스는 매칭 작업을 수행하는 엔진을 대표합니다. 이 클래스는 생성자가 없으며, java.util.regex.Matcher의matches()
java.util.regex.Pattern 클래스의 메서드를 생성/객체를 얻습니다.
매칭이 되면 이(Matcher) 클래스의requireEnd()메서드는 더 많은 입력이 있는 경우 일치 결과가 거짓인 기회가 있는지 확인하고, 있으면 true를 반환하며, 없으면 false를 반환합니다.
예를 들어, 입력 문자열의 마지막 단어를 "you $" 정규 표현식으로 일치시키려고 시도할 때는, 첫 번째 입력 행이 "hello you are"라면 일치할 수 있지만, 더 많은 문장의 마지막 단어가 필요한 단어가 아닐 수 있으며(즉 "you"), 매칭 결과가 거짓이 될 수 있습니다. 이 경우, 메서드는requiredEnd()
메서드는 true를 반환합니다.
또한, 특정 문자를 입력에 일치시키려고 시도할 때는 '#'을 말하고, 첫 번째 입력 행이 "Hello#안녕하세요"라면 일치 합니다. 더 많은 입력 데이터가 매칭 엔진의 내용을 변경할 수 있지만 결과를 변경하지 않습니다. 이 경우, 메서드는requiredEnd()
메서드는 false를 반환합니다.
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RequiredEndExample { public static void main(String args[]) { String regex = "you$"; //사용자 입력을 읽습니다 Scanner sc = new Scanner(System.in); System.out.println("입력 텍스트를 입력하세요:"); String input = sc.nextLine(); //Pattern 클래스를 인스턴스화 Pattern pattern = Pattern.compile(regex); //Matcher 클래스를 인스턴스화 Matcher matcher = pattern.matcher(input); //일치가 발생했는지 확인 if(matcher.find()) { System.out.println("일치 발견"); } boolean result = matcher.requireEnd(); if(result) { System.out.println("더 많은 입력이 일치 결과를 false로 변환할 수 있습니다"); } else{ System.out.println("일치 결과는 더 많은 데이터가 있더라도 true로 나타날 것입니다"); } } }
출력 결과
입력 텍스트를 입력하세요: Hello how are you 일치 발견 추가 입력이 결과를 거짓으로 만들 수 있습니다
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RequiredEndExample { public static void main(String args[]) { String regex = "[#]"; //사용자 입력을 읽습니다 Scanner sc = new Scanner(System.in); System.out.println("입력 텍스트를 입력하세요:"); String input = sc.nextLine(); //Pattern 클래스를 인스턴스화 Pattern pattern = Pattern.compile(regex); //Matcher 클래스를 인스턴스화 Matcher matcher = pattern.matcher(input); //일치가 발생했는지 확인 if(matcher.find()) { System.out.println("일치 발견"); } boolean result = matcher.requireEnd(); if(result) { System.out.println("더 많은 입력이 일치 결과를 false로 변환할 수 있습니다"); } else{ System.out.println("일치 결과는 더 많은 데이터가 있더라도 true로 나타날 것입니다"); } } }
출력 결과
입력 텍스트를 입력하세요: Hello# how# you 일치 발견 일치 결과는 더 많은 데이터가 있더라도 true로 나타날 것입니다