English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
java.util.regex.Matcher 클래스는 여러 가지 매칭 작업을 수행하는 엔진을 나타냅니다. 이 클래스는 생성자가 없으며, 사용할 수 있습니다.matches()
java.util.regex.Pattern 클래스의 메서드를 생성/해당 클래스의 객체를 얻습니다.
이러한 것들의find()이 메서드는 현재 매치어 객체와 일치하는 다음 입력을 탐색하려고 시도합니다. 일치하면 이 메서드는 true를 반환하고, 일치하지 않으면 false를 반환합니다。
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class FindExample { public static void main( String args[] ) { //문자열 값을 읽기 Scanner sc = new Scanner(System.in); System.out.println("Enter input string"); String input = sc.nextLine(); //숫자를 찾는 정규 표현식 String regex = "(\\D)"; //정규 표현식 컴파일 Pattern pattern = Pattern.compile(regex); //매치어 객체 검색 Matcher matcher = pattern.matcher(input); //일치 여부 확인 if(matcher.find()) { System.out.println("Given string contain non-digit characters"); } else { System.out.println("Given string does not contain non-digit characters"); } } }
출력 결과
Enter input string 11245# Given string contain non-digit characters