728x90

 

메타문자
\d 숫자
\D 숫자가 아니다
\w 문자 또는 숫자
\W 문자 또는 숫자가 아니다
\s 공백
\S 공백이 아닌 것
a-z a ~ z 사이에 있는 모든 소문자 알파벳
A-Z A ~ Z 사이에 있는 모든 대문자 알파벳
가-힣 한글 문자
0-9 0부터 9까지의 모든 숫자
[] []로 감싼 메타문자에 일치하는 한개의 문자
[abc] : a,b,c 중 하나의 문자 
[^abcabc] : a,b,c 가 아닌 문자 하나
() 그룹 지정
.* 모든 문자열
c. c로 시작하는 두자리 문자열
c.* c로 시작하는 모든 문자열
c\. c.와 일치하는 문자열 (문자열 이스케이프 \)
[b|c].*
[bc].*
[b-c].*
b 또는 c로 시작하는 문자열
c\w c로 시작하는 문자 또는 숫자
^ 문자열의 시작
$ 문자열의 마지막
[^0-9] 숫자가 아닌 글자
메타문자 횟수
* 0번 이상
+ 1번 이상
? 0번이거나 1번
{n} n번
{n,} n번 이상
{n, m} n번 이상 m번 이하
. 1번
.{2} 문자2개
   
   

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.java.study.first.reg;
 
import java.util.regex.Pattern;
 
public class Reg01 {
    public static void main(String[] args) {
 
 
        // * 첫문자는 A ~ Z 사이의 한글자
        // * 두번째 문자부터 a~z ㄸ는 A~Z사이의 문자가 0개이상
        boolean test1 = Pattern.matches("[A-Z][a-zA-Z]*""Apple");
        System.out.println("test1 = " + test1);
 
 
 
 
        // * 시작하는 문자는 알파벳 1개 이상
        // * 다음에는 '나 -가 한번 나오고, 알파벳은 1번이상오는  <- 이게 하나의 그륩으로 0번이상 나온다
        boolean test2 = Pattern.matches("[a-zA-Z]+(['-][a-zA-Z]+)*""Apple's");
        System.out.println("test2 = " + test2);
 
        boolean test3 = Pattern.matches("[a-zA-Z]+(['-][a-zA-Z]+)*""Apple-Tea");
        System.out.println("test3 = " + test3);
 
 
 
        // * 첫문자는 ' 또는 - 가 나올수 있고
        // * 두번째 문자부터는 알파벳이 1개이상 올수 있다
        boolean test4 = Pattern.matches("['-][a-zA-Z]+""-Hello");
        System.out.println("test4 = " + test4);
 
        boolean test5 = Pattern.matches("['-][a-zA-Z]+""'this");
        System.out.println("test5 = " + test5);
 
 
 
        // * 핸드폰
        boolean test6 = Pattern.matches("01[0-9]-\\d{3,4}-\\d{4}""010-1234-5678");
        System.out.println("test6 = " + test6);
 
 
        // * 이메일
        boolean test7 = Pattern.matches("^\\w+@\\w+(\\.\\w+){1,2}$""blatrem@naver.com");
        System.out.println("test7 = " + test7);
 
        boolean test8 = Pattern.matches("\\w+@\\w+(\\.\\w+){1,2}""smathj@natae.co.kr");
        System.out.println("test8 = " + test8);
 
 
        // * 주민등록번호 123456-1234567 6자리, 7자리
        boolean test9 = Pattern.matches("\\d{2}[0-1][0-9][0-3][0-9]-[1-4]\\d{6}""123456-1234567");
        System.out.println("test9 = " + test9);
 
        boolean test10 = Pattern.matches("\\d{6}-[1-4]\\d{6}""123456-1234567");
        System.out.println("test10 = " + test10);
    }
}
 
cs
728x90

'개발 > java' 카테고리의 다른 글

Stream 람다식을 이용한 정렬  (0) 2024.02.27
배포시 환경설정 하기  (1) 2023.12.03
자바의 어노테이션  (0) 2021.09.15
람다식 사용법에 대해서 알아보자  (1) 2021.04.12
기본입출력으로 파일 복사하기  (0) 2021.04.04

+ Recent posts