English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Java String Match() 메서드는 문자열이 주어진 정규 표현식과 일치하는지 확인합니다.
문자열 matches() 메서드의 문법은 다음과 같습니다:
string.matches(String regex)
여기서 string은 String 클래스의 객체입니다.
regex - 정규 표현식
정규 표현식이 문자열과 일치하면true를 반환
정규 표현식이 문자열과 일치하지 않으면false를 반환
class Main { public static void main(String[] args) { //정규 표현식 패턴 //a로 시작하고 s로 끝나는 다섯 글자 문자열 String regex = "^a...s$"; System.out.println("abs".matches(regex)); // false System.out.println("alias".matches(regex)); // true System.out.println("an abacus".matches(regex)); // false System.out.println("abyss".matches(regex)); // true } }
여기 "^a...s$"는 정규 표현식으로, 시작하는 a와 끝나는 s로 구성된5개의 문자를 포함한 문자열 s。
//문자열이 숫자만으로 구성되었는지 확인 class Main { public static void main(String[] args) { //숫자만 검색하는 패턴 String regex = "^[0-9]+$"; System.out.println("123a".matches(regex)); // false System.out.println("98416".matches(regex)); // true System.out.println("98 41".matches(regex)); // false } }
여기 "^[0-9]+$"은 정규 표현식으로, 숫자만을 나타냅니다.