본문 바로가기

WEB_Programming/Pure Java

Regular Expression > Predefined Character Classes


Pattern API에는 몇가지 유용한 predefined character class가 있다. 이것은 공통적으로 regular expression에서 이용되는 짧은 표기식이다.
Predefined Character Classes
. Any character (may or may not match line terminators)
\d A digit: [0-9]
\D A non-digit: [^0-9]
\s A whitespace character: [ \t\n\x0B\f\r]
\S A non-whitespace character: [^\s]
\w A word character: [a-zA-Z_0-9]
\W A non-word character: [^\w]
상단 테이블에서와 같이, 왼쪽 칼럼의 명령은 오른쪽 칼럼에서 의미하는 내용을 짧은 표기식으로 쓰는 방법이다.
예를 들어 \d은 (0-9)사이의 숫자 범위를 나타낸다. \w의 의미는 (소문자, 대문자, _, 숫자)에 해당하는 어떤 문자든지 올수 잇음을 의미한다. 가능한한 predefined class를 이용하는것이 좋다. 이것은 코드를 읽기 쉽게 만들고, 관리하거나, 오류를 쉽게 찾아낼 수 있는 방법을 제공해준다.

생성자에서 백슬레시를 사용하는 경우 이것을 excaped construct라고 부른다. 우리는 이스케이프 문자를 String Literals 섹션에서 확인했었고, \Q and \E 쿼테이션을 표기하기 위해서 이것을 이용했었다. 만약에 이스케이프 생성자를 이용하는경우 백슬레시를 컴파일을 위해서 하나더 써줘야 한다. 예를 들면..

private final String REGEX = "\\d"; // a single digit

이 예에서는 \d라는 정규식 표현을 이용하고 잇다. 추가적인 백슬레시는 코드 컴파일을 위해서 해주어야 한다. test harness에서는 표현식을 콘솔에서 바로 읽어오기 때문에 추가적인 백슬레시가 필요 없게 된다.

다음 예제는 predefined character classes의 사용방법을 보여준다.

Enter your regex: .
Enter input string to search: @
I found the text "@" starting at index 0 and ending at index 1.

Enter your regex: .
Enter input string to search: 1
I found the text "1" starting at index 0 and ending at index 1.

Enter your regex: .
Enter input string to search: a
I found the text "a" starting at index 0 and ending at index 1.

Enter your regex: \d
Enter input string to search: 1
I found the text "1" starting at index 0 and ending at index 1.

Enter your regex: \d
Enter input string to search: a
No match found.

Enter your regex: \D
Enter input string to search: 1
No match found.

Enter your regex: \D
Enter input string to search: a
I found the text "a" starting at index 0 and ending at index 1.

Enter your regex: \s
Enter input string to search:
I found the text " " starting at index 0 and ending at index 1.

Enter your regex: \s
Enter input string to search: a
No match found.

Enter your regex: \S
Enter input string to search:
No match found.

Enter your regex: \S
Enter input string to search: a
I found the text "a" starting at index 0 and ending at index 1.

Enter your regex: \w
Enter input string to search: a
I found the text "a" starting at index 0 and ending at index 1.

Enter your regex: \w
Enter input string to search: !
No match found.

Enter your regex: \W
Enter input string to search: a
No match found.

Enter your regex: \W
Enter input string to search: !
I found the text "!" starting at index 0 and ending at index 1.

첫번째 3개의 예제는 정규식 표현을 단순히 . ("dot" 메타 캐릭터)을 이용하여 "어떠한 문자든"이라고 정해주고 있다.
그러므로 문자, 숫자, @문자든 모두 매치에 성공하게 된다. 나머지 예제는 단일 정규식 표현을 생성하는 것으로 Predefined Character Classes table 을 참조하여 생성하고 있다. 각 매치는 다음과 같다.
  • \d matches all digits : 모든 숫자
  • \s matches spaces : 공백 문자에 해당하는 것
  • \w matches word characters : 단어
반대 케이스는 다음과 같다.
  • \D matches non-digits : 숫자가 아닌 매치
  • \S matches non-spaces : 공백이 아닌 문자와 매치
  • \W matches non-word characters : 단어가 아닌것들과 매치 (특수문자... 등등..)