개발입문/JAVA

Regular Expression 정규표현식(1) Character

haloaround 2017. 9. 19. 23:46

이 포스트는 Character 하나 하나를 표기하는 방법에 대해 정리합니다.

여기서 Character 는 글자, 숫자, 공백 모두를 포함합니다.


컴퓨터(컴파일러) 는 String 문자열을 char [] 로 인식합니다.

즉, 우리도 글자마다의 조합으로 패턴을 구성해야 합니다. 



Meta Character

특별한 뜻 (정해진 규칙) 을 가지는 캐릭터 들입니다.

만약 정해진 규칙을 escape 하려면 \ 를 앞에 붙이거나, \Q ... \E 로 감싸야 한다.

- 괄호  < ( { [ ] } ) >

- 특수문자  ^ - = $ ! | ? * + .



 Character Class

[ ] 안의 규칙은 모두 한 글자에 대한 규칙을 명세한다. 

요 규칙들을 클래스라고 지칭합니다. (a.class 아니고!)


[논리연산자]

집합을 생각하면 쉬운 개념입니다.

^  : 부정, negation

-  : 범위 range

&&  : 교집합 intersection

[   [ ]]  : 합집합 union

[  &&[^  ]] : 차집합 subtracton




ConstructDescription
[abc]a, b, or c (simple class)
[^abc]Any character except a, b, or c (negation)
[a-zA-Z]a through z, or A through Z, inclusive (range)
[a-d[m-p]]a through d, or m through p: [a-dm-p] (union)
[a-z&&[def]]d, e, or f (intersection)
[a-z&&[^bc]]a through z, except for b and c: [ad-z] (subtraction)
[a-z&&[^m-p]]a through z, and not m through p: [a-lq-z] (subtraction)




Predefined Character

아래 표기들은 숫자, 공백, 문자가 들어온다는 약속을 지정해놓았습니다.

그리고 . 은 숫자,공백,문자 무엇이던지 어느 한 글자가 있어야함을 의미합니다.

.   아무거나 한글자

d  숫자 digit

s  공백 space

w  글자(알파벳, 숫자, 공백) word 

대문자 반대, \D 는 \d 숫자캐릭터의 반대


ConstructDescription
.Any character (may or may not match line terminators)
\dA digit: [0-9]
\DA non-digit: [^0-9]
\sA whitespace character: [ \t\n\x0B\f\r]
\SA non-whitespace character: [^\s]
\wA word character: [a-zA-Z_0-9]
\WA non-word character: [^\w]





[ 정규표현식 포스트 ]

1. Character Classes
글자, 숫자, 공백 등을 표시하는 기법, [ ] 캐릭터 하나, ( ) 캐릭터 여러개
http://haloaround.tistory.com/admin/entry/post/?id=183

2. Quandifiers 
캐릭터가 몇번 반복되는지, 그리고 어떻게 이런것들을 검색하는지
http://haloaround.tistory.com/admin/entry/post/?id=184

3. Capturing Groups & Boundary Match
매칭단위는 무엇이고 매칭결과를 어디에서 찾아야하는지 등의 검색 조건을 지정하는 기법
http://haloaround.tistory.com/admin/entry/post/?id=185

4. Java Pattern and Matcher Class
Input Sequence 리소스에서 Pattern 에 부합하는 것을 검색하는 기능
http://haloaround.tistory.com/admin/entry/post/?id=186