본문 바로가기

JAVA/Regex

정규표현식 표로 정리해보자 + 자주 쓰는 정규표현식 모음

자주 쓰는 표현식은 지속적으로 추가예정

1. 기본 패턴

정규표현식 패턴설명예시

\ 이스케이프 문자: 특수 문자를 문자 그대로 사용하게 함 "\.는 "." 문자와 매칭"
. 임의의 한 문자 (줄 바꿈 제외) "a.b"는 "acb", "a9b"와 매칭
\d 숫자 (0-9) "\\d"는 "7", "3"와 매칭
\D 숫자가 아닌 문자 "\\D"는 "a", "!"와 매칭
\w 알파벳, 숫자, 밑줄 "\\w"는 "A", "7", "_"와 매칭
\W 알파벳, 숫자가 아닌 문자 "\\W"는 "%", " "와 매칭
\s 공백 문자 (스페이스, 탭, 줄 바꿈) "\\s"는 " "(공백), "\t", "\n"와 매칭
\S 공백이 아닌 문자 "\\S"는 "a", "1", "!"와 매칭
^ 문자열의 시작 "^a"는 "apple"과 매칭
[^] 부정 문자 클래스: 대괄호 안의 문자를 제외한 것 "[^abc]는 "d", "e" 등과 매칭"
[] 문자 클래스: 대괄호 안의 문자 중 하나 "[abc]는 "a", "b", "c"와 매칭"
$ 문자열의 끝 "end$"는 "the end"와 매칭
\b 단어 경계 "\bis\b"는 "this is it"에서 "is"와 매칭
\B  단어 경계가 아님 (non-word boundary) "\Bis\B는 "mississippi"에서 "is"와 매칭"

2. 반복 패턴

정규표현식 패턴설명예시

* 앞 문자가 0번 이상 반복 "a*"는 "", "a", "aaaa"와 매칭
+ 앞 문자가 1번 이상 반복 "a+"는 "a", "aaa"와 매칭
? 앞 문자가 0번 또는 1번 반복 "a?"는 "", "a"와 매칭
{n} 정확히 n번 반복 "a{3}"는 "aaa"와 매칭
{n,} n번 이상 반복 "a{2,}"는 "aa", "aaaaa"와 매칭
{n,m} n번 이상 m번 이하 반복 "a{2,4}"는 "aa", "aaa", "aaaa"와 매칭

3. 그룹과 선택

정규표현식 패턴설명예시

() 그룹화 "(abc)"는 "abc"와 매칭
` ` OR 조건 (A 또는 B)
`(a b c)`

4. 문자 클래스

정규표현식 패턴설명예시

[abc] a 또는 b 또는 c "[abc]"는 "a", "b", "c"와 매칭
[^abc] a, b, c가 아닌 문자 "[^abc]"는 "d", "1" 등과 매칭
[a-z] 소문자 a부터 z "[a-z]"는 "a", "z"와 매칭
[A-Z] 대문자 A부터 Z "[A-Z]"는 "A", "Z"와 매칭
[0-9] 숫자 0부터 9 "[0-9]"는 "0", "9"와 매칭
[a-zA-Z0-9] 알파벳 대소문자 및 숫자 "[a-zA-Z0-9]"는 "a", "A", "5"와 매칭

 

정규표현식 특수문자 의미 정리

     
[] 문자 클래스: 대괄호 안의 문자 중 하나 [abc]는 "a", "b", "c"와 매칭
[^] 부정 문자 클래스: 대괄호 안의 문자를 제외한 것 [^abc]는 "d", "e" 등과 매칭
\ 이스케이프 문자: 특수 문자를 문자 그대로 사용하게 함 \.는 "." 문자와 매칭

 

5. 자바에서 사용하는 정규표현식 예시

1. 이메일 주소 검증

 
String emailPattern = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$";
Pattern pattern = Pattern.compile(emailPattern);
Matcher matcher = pattern.matcher("example@test.com");
boolean isValid = matcher.matches(); // true

2. 전화번호 형식 검증 (한국 전화번호)

String phonePattern = "^\\d{2,3}-\\d{3,4}-\\d{4}$";
Pattern pattern = Pattern.compile(phonePattern);
Matcher matcher = pattern.matcher("010-1234-5678");
boolean isValid = matcher.matches(); // true

2-1. 전화번호 형식 검증 (한국 전화번호 -  선택 포함)

String phonePattern = "^\\d{2,3}-?\\d{3,4}-?\\d{4}$";
Pattern pattern = Pattern.compile(phonePattern);
Matcher matcher = pattern.matcher("010-1234-5678"); // 또는 "01012345678"
boolean isValid = matcher.matches(); // true

 

3. 비밀번호 형식 (영문, 숫자 포함 8-16자)

 
java
코드 복사
String passwordPattern = "^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,16}$"; Pattern pattern = Pattern.compile(passwordPattern); Matcher matcher = pattern.matcher("abc12345"); boolean isValid = matcher.matches(); // true

3-1. 비밀번호 형식 (영문, 숫자, 특수문자 포함 8-16자)

String passwordPattern = "^(?=.*[A-Za-z])(?=.*\\d)(?=.*[@$!%*#?&])[A-Za-z\\d@$!%*#?&]{8,16}$";
Pattern pattern = Pattern.compile(passwordPattern);
Matcher matcher = pattern.matcher("abc12345!");
boolean isValid = matcher.matches(); // true

 

4. 공백 제거

java
코드 복사
String result = input.replaceAll("\\s+", ""); // 문자열의 모든 공백 제거

5. URL 검증

java
코드 복사
String urlPattern = "^(http|https)://[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}(/.*)?$"; Pattern pattern = Pattern.compile(urlPattern); Matcher matcher = pattern.matcher("https://www.example.com"); boolean isValid = matcher.matches(); // true

6. 유용한 메소드

  • Pattern.compile(String regex) : 정규표현식을 컴파일하여 패턴을 생성
  • Matcher.matches() : 전체 문자열이 정규표현식과 일치하는지 확인
  • Matcher.find() : 입력 문자열 내에서 일치하는 부분을 찾음
  • Matcher.group() : 일치하는 그룹을 반환
  • Matcher.replaceAll(String replacement) : 일치하는 모든 부분을 치환

'JAVA > Regex' 카테고리의 다른 글

자바 이스케이프 처리  (4) 2024.10.17
Pattern, Matcher 정리  (1) 2024.10.17