Regular Expression 정규표현식(3) Capturing Group and Boundarys Matchers
Capturing Groups
수학에서 ( ) 괄호로 감싸진 것은 먼저 계산해야하는 것처럼
정규표현식에서 ( ) 로 감싸진 것은 한 계산단위 unit 으로 인식합니다.
Quantifier 적용도 이 ( ) 단위로 적용한다.
((A)(B(C))) 의 경우 다음 순으로 연산합니다.
) 를 만난 순이라고 생각하면 쉬워요.
((A)(B(C)))
(A) --> (C) --> (B(C)) --> ((A)(B(C)))
Back References
\ backslash 로 표현됩니다.
(\d\d)\1 는
(\d\d) 두 숫자가
\1 :1번 더 반복된다는 뜻!
Enter your regex: (\d\d)\1 Enter input string to search: 1212 I found the text "1212" starting at index 0 and ending at index 4.
Boundary Matchers
검색위치에 만족하는 매칭결과만 매칭됩니다.
즉, 매칭결과가 어디에서 나타나는지를 제한할 수 있는 구문입니다.
문장맨앞 ^, 문장맨뒤 $,
단어단위 \b, 입력소스맨앞 \A, 이전매칭결과바로뒤 \G 등등
Boundary Construct | Description |
---|---|
^ | The beginning of a line |
$ | The end of a line |
\b | A word boundary |
\B | A non-word boundary |
\A | The beginning of the input |
\G | The end of the previous match |
\Z | The end of the input but for the final terminator, if any |
\z | The end of the input |
Enter your regex: ^dog$ Enter input string to search: dog I found the text "dog" starting at index 0 and ending at index 3. Enter your regex: ^dog$ Enter input string to search: dog No match found. Enter your regex: \s*dog$ Enter input string to search: dog I found the text " dog" starting at index 0 and ending at index 15. Enter your regex: ^dog\w* Enter input string to search: dogblahblah I found the text "dogblahblah" starting at index 0 and ending at index 11.
dog로 문장시작, 끝
\s* 공백이 앞에 있거나없거나 해도 되는데, dog 로 문장이 끝나는 매칭
dog 로 문장이 시작하고 뒤에 글자가 마구 들어와도 되는 매칭
[ 정규표현식 포스트 ]
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