String matches() method

22·2023년 12월 7일

Java

목록 보기
2/3

"joy12***3" 이라는 마스킹된 id 가 있다. 이런 경우, "joy12kkk3" 일수도 "joy12aaa3" 이 될 수도 있다. 이러한 여러 가지 경우의 수를 String matches 메서드로 쉽게 찾을 수 있다.

matches()

이 메서드는 주어진 정규 표현식과 일치하는지 여부를 나타낸다. str.matches(regex) 형식의 이 메서드 호출은 정확히 Pattern.matches(regex, str) 표현식과 동일한 결과를 나타낸다.

String id ="joy12***3".replace('*', '.'); // 마스킹된 ***을 .으로 바꾼다.
System.out.println("joy120003".matches(id)); // true 

정규식
. => 어떤 문자 하나를 나타낸다.
.* => 모든 문자(0개 이상)이 올 수 있다.

String Str = "Welcome to Korea!!!";

System.out.println(Str.matches(".*Korea.*")); // true
System.out.println(Str.matches("Korea"));     // false
System.out.println(Str.matches(".Korea."));	  // false
System.out.println(Str.matches("Korea(.*)")); // true

참고

https://www.tutorialspoint.com/java/java_string_matches.htm

0개의 댓글