본문 바로가기

WEB_Programming/Pure Java

Regular Expression > Character Classes

Pattern 클래스의 스펙을 통해서 지원되는 정규식 생성자를 보고자 한다면 다음 테이블에 나와있다. Character Classes 섹션에서 다음 내용에 대해서 설명할 것이다.
Character Classes
[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)
왼쪽 칼럽 지정자는 정규식의 생성 명령이다. 오른쪽의 내용은 이러한 생성자가 match를 수행할때 어떻게 처리를 하는지 묘사한 것이다.
Note: character class 문장에서 class라는 단어는 .class파일을 의미하는것이 아니다. 정규식에서 컨텍스트를 말하는 것으로 character class는 []사이에 들어있는 문자 셋을 의미한다. 이것은 주어진 문자열에서 한 문자에 대해서 성공적인 매칭을 위해서 특별히 지정하는 의미이다.

Simple Classes

문자 클래스의 가장 기본적인 형태로 [] 안에 문자를 나열하는 것이다. 예를 들어 정규식 [bcr]은 매치되는 단어라 "bat", "cat", "rat"가 매칭된다. 이것이 정상적으로 매칭되는 이유는 "b", "c", "r"이 처음 단어로 매칭되기 때문이다.

Enter your regex: [bcr]at
Enter input string to search: bat
I found the text "bat" starting at index 0 and ending at index 3.

Enter your regex: [bcr]at
Enter input string to search: cat
I found the text "cat" starting at index 0 and ending at index 3.

Enter your regex: [bcr]at
Enter input string to search: rat
I found the text "rat" starting at index 0 and ending at index 3.

Enter your regex: [bcr]at
Enter input string to search: hat
No match found.

상단의 예제처럼 첫번째 문자 매칭에서 캐릭터 클래스에 의해서 정의된 캐릭터 문자에 의해서 성공적으로 매칭 작업이 수행된다.

Negation

정규식 표현 앞에 "^"메타문자를 입력하면 이후 모든 단어는 제외하르는 의미가 된다. 이 기술은 negation이라고 알려져 있다.

Enter your regex: [^bcr]at

Enter input string to search: bat

No match found.


Enter your regex: [^bcr]at

Enter input string to search: cat

No match found.


Enter your regex: [^bcr]at

Enter input string to search: rat

No match found.


Enter your regex: [^bcr]at

Enter input string to search: hat

I found the text "hat" starting at index 0 and ending at index 3.

캐릭터 클래스에 정의된 단어가 입력 문자의 처음에 나오지 않는경우 성공적으로 매치 작업이 수행된다.

Ranges

가끔 특정 범위의 문자와 매칭을 수행해야할 경우가 있다. "a"에서 "h"까지 단어나 혹은 "1" 에서 "5"까지 숫자를 검색하고자 하는 경우, 단순하게 "-"단어를 첫번째 글자와, 끝 글자 사이에 넣어주면 된다. [1-5] 혹은 [a-h]라고 지정해주면 된다. 또한 서로다른 범위의 문자를 매칭 시킬수도 있다. 예를 들어 [a-zA-z]라고 해주면 알파벳의 a에서 z까지 소문자와, A에서 Z까지 대문자 에 해당하는 단어를 검사하게된다.

range와 negation에 대한 예를 살펴보자

Enter your regex: [a-c]

Enter input string to search: a

I found the text "a" starting at index 0 and ending at index 1.


Enter your regex: [a-c]

Enter input string to search: b

I found the text "b" starting at index 0 and ending at index 1.


Enter your regex: [a-c]

Enter input string to search: c

I found the text "c" starting at index 0 and ending at index 1.


Enter your regex: [a-c]

Enter input string to search: d

No match found.


Enter your regex: foo[1-5]

Enter input string to search: foo1

I found the text "foo1" starting at index 0 and ending at index 4.


Enter your regex: foo[1-5]

Enter input string to search: foo5

I found the text "foo5" starting at index 0 and ending at index 4.


Enter your regex: foo[1-5]

Enter input string to search: foo6

No match found.


Enter your regex: foo[^1-5]

Enter input string to search: foo1

No match found.


Enter your regex: foo[^1-5]

Enter input string to search: foo6

I found the text "foo6" starting at index 0 and ending at index 4.

Unions

unions를 이용하여 단일 문자를 두개 혹은 여러개 구분 문자 클래스들과 함께 사용할 수있다. 유니온을 생성하는 방법은 단순하게 [0-4[6-8]]의 형식으로 해주면 된다. 0, 1, 2, 3, 4, 6, 7, 8에 해당하는 문자를 찾는 가장 간단한 유니온 방법이다.

Enter your regex: [0-4[6-8]]

Enter input string to search: 0

I found the text "0" starting at index 0 and ending at index 1.


Enter your regex: [0-4[6-8]]

Enter input string to search: 5

No match found.


Enter your regex: [0-4[6-8]]

Enter input string to search: 6

I found the text "6" starting at index 0 and ending at index 1.


Enter your regex: [0-4[6-8]]

Enter input string to search: 8

I found the text "8" starting at index 0 and ending at index 1.


Enter your regex: [0-4[6-8]]

Enter input string to search: 9

No match found.

Intersections

하나의 캐릭터 클래스와 포함된 캐릭터 클래스에서 공통되는 캐릭터 문자만을 매치하고 싶은경우 && 를 이용할 수 있으며 [0-9&&[345]]로 사용할 수 있다. 이 부분적인 인터섹션을 생성하면, 3, 4, 5에 해당하는 문자만 매칭을 수행한다.

Enter your regex: [0-9&&[345]]

Enter input string to search: 3

I found the text "3" starting at index 0 and ending at index 1.


Enter your regex: [0-9&&[345]]

Enter input string to search: 4

I found the text "4" starting at index 0 and ending at index 1.


Enter your regex: [0-9&&[345]]

Enter input string to search: 5

I found the text "5" starting at index 0 and ending at index 1.


Enter your regex: [0-9&&[345]]

Enter input string to search: 2

No match found.


Enter your regex: [0-9&&[345]]

Enter input string to search: 6

No match found.


두개의 범위에서 인터섹션에 대한 예를 여기서 볼 수 있다.

Enter your regex: [2-8&&[4-6]]

Enter input string to search: 3

No match found.


Enter your regex: [2-8&&[4-6]]

Enter input string to search: 4

I found the text "4" starting at index 0 and ending at index 1.


Enter your regex: [2-8&&[4-6]]

Enter input string to search: 5

I found the text "5" starting at index 0 and ending at index 1.


Enter your regex: [2-8&&[4-6]]

Enter input string to search: 6

I found the text "6" starting at index 0 and ending at index 1.


Enter your regex: [2-8&&[4-6]]

Enter input string to search: 7

No match found.

Subtraction

마지막으로 substraction 은 negate 를 하나 혹은 이상의 포함된 캐릭터 클래스에 수행하는 것이다. 이것은 [0-9&&[^345]]로 표현할 수 있으며, 이 예제에서는 단일 클래스를 0 에서 9까지 매치되는 문자중에서 3, 4를 제외한 내용을 의미한다.

Enter your regex: [0-9&&[^345]]

Enter input string to search: 2

I found the text "2" starting at index 0 and ending at index 1.


Enter your regex: [0-9&&[^345]]

Enter input string to search: 3

No match found.


Enter your regex: [0-9&&[^345]]

Enter input string to search: 4

No match found.


Enter your regex: [0-9&&[^345]]

Enter input string to search: 5

No match found.


Enter your regex: [0-9&&[^345]]

Enter input string to search: 6

I found the text "6" starting at index 0 and ending at index 1.


Enter your regex: [0-9&&[^345]]

Enter input string to search: 9

I found the text "9" starting at index 0 and ending at index 1.


이제 우리는 어떻게 캐릭터 클래스를 생성하는지 확인했다. Character Classes table를 확인해보고 다음 섹션으로 넘어가기 바란다.