정규표현식 써보기
[] 안에서의 ^
해당 문자를 제외한 문자 중 하나와 일치
public class Main {
public static void main(String[] args) {
String num = "123854-4565156";
num = num.replaceAll("[^-]", "*");
System.out.println(num);
}
}
[] 안의 "-"를 제외한 문자들이 모두 "*"로 변환되었다.
^
[]밖에서는 문자열의 시작 부분을 나타낸다.
public class Main {
public static void main(String[] args) {
String str1 = "abcde";
String str2 = "bcde";
String str3 = "apple";
boolean matches1 = str1.matches("^a....");
System.out.println("str1 : " + str1);
System.out.println("regex : ^a....");
System.out.println("result: " + matches1);
System.out.println();
boolean matches2 = str2.matches("^b[a-z][a-z][a-z]");
System.out.println("str2 : " + str2);
System.out.println("regex : ^b[a-z][a-z][a-z]");
System.out.println("result: " + matches2);
System.out.println();
boolean matches3 = str3.matches("[a-z]^[p][p][l][e]");
System.out.println("str3 : " + str3);
System.out.println("regex : [a-z]^[p][p][l][e]");
System.out.println("result: " + matches3);
System.out.println();
boolean matches4 = str3.matches("^[a-z][p][p][l][e]");
System.out.println("str3 : " + str3);
System.out.println("regex : ^[a-z][p][p][l][e]");
System.out.println("result: " + matches4);
System.out.println();
}
}
'^'뒤에 문자로 시작하는 문자라는 의미
.
임의의 문자 하나를 나타낸다. 단, 줄 바꿈 문자(\n)는 제외
public class Main {
public static void main(String[] args) {
String str1 = "abc";
String str2 = "adc";
String str3 = "aec";
String str4 = "ac";
String str5 = "abbc";
String str6 = "accc";
String str7 = "adec";
String str8 ="a\nc";
boolean matches1 = str1.matches("a.c");
System.out.println("str1 : " + str1);
System.out.println("matches1 : " + matches1);
boolean matches2 = str2.matches("a.c");
System.out.println("str2 : " + str2);
System.out.println("matches2 : " + matches2);
boolean matches3 = str3.matches("a.c");
System.out.println("str3 : " + str3);
System.out.println("matches3 : " + matches3);
boolean matches4 = str4.matches("a.c");
System.out.println("str4 : " + str4);
System.out.println("matches4 : " + matches4);
boolean matches5 = str5.matches("a.c");
System.out.println("str5 : " + str5);
System.out.println("matches5 : " + matches5);
boolean matches6 = str6.matches("a.c");
System.out.println("str6 : " + str6);
System.out.println("matches6 : " + matches6);
boolean matches7 = str7.matches("a..c");
System.out.println("str7 : " + str7);
System.out.println("matches7 : "+ matches7);
boolean matches8 = str8.matches("a.c");
System.out.println("str8 : " + str8);
System.out.println("matches8 : "+ matches8);
}
}
[]
대괄호 안에 들어가는 문자 중 하나와 일치하는지
public class Main {
public static void main(String[] args) {
String str1 = "d";
String str2 = "ad";
String str3 = "apple";
String str4 = "[";
String str5 = "[apple]";
boolean matches = str1.matches("[acd]");
System.out.println("str1 : " + str1);
System.out.println("regex : [acd]");
System.out.println("result : " + matches);
System.out.println();
boolean matches2 = str2.matches("[a-c][b-d]");
System.out.println("str2 : " + str2);
System.out.println("regex : [a-c][b-d]");
System.out.println("result : " + matches2);
System.out.println();
boolean matches3 = str3.matches("[a][p][p][l][e]");
System.out.println("str3 : " + str3);
System.out.println("regex : [a][p][p][l][e]");
System.out.println("result : " + matches3);
System.out.println();
boolean matches4 = str3.matches("[apple]");
System.out.println("str3 : " + str3);
System.out.println("regex : [apple]");
System.out.println("result : " + matches4);
System.out.println();
boolean matches5 = str4.matches("[\\[]");
System.out.println("str4 : " + str4);
System.out.println("regex : [\\[]");
System.out.println("result : " + matches5);
System.out.println();
boolean matches6 = str5.matches("[\\[][a][p][p][a-z][a-z].");
System.out.println("str5 : " + str5);
System.out.println("regex : [\\[][a][p][p][a-z][a-z].");
System.out.println("result : " + matches6);
System.out.println();
}
}
대괄호를 써보며 false만 맞닥 뜨리다가
이것저것 넣어보며 이제는 정확하게 이해하게 되었다.
matches() 메서드의 괄호 안의 값과 정확하게 일치가 되어야
true를 반환한다.
'자바 탐구' 카테고리의 다른 글
JWT) JWT 사용해보기 (0) | 2023.04.19 |
---|---|
정규표현식) 메타문자 - * + () 써보기 (0) | 2023.04.12 |
자바) JVM (0) | 2023.04.09 |
자바) 객체 지향 프로그래밍 (0) | 2023.04.09 |
Intellij) 테마 바꾸기 (0) | 2023.04.03 |