정규식 정복하기1 - ZVON, Regular Expression Tutorial

정규식 정복하기1 - ZVON, Regular Expression Tutorial

Rege, Regex. 정규식을 공부해보자

만들고 있는 앱에서 Search를 할 일이 있는데, 이참에 제대로 정규표현식 공부를 해보면 좋을 것 같아서 공부하고 이해한 내용을 정리해보고자 한다.

몇 가지 글을 읽어보았는데, 망각의 동물의 대표격인 나로서는 여러 표현 문법자체를 달달 외우고 공부하는 것은 비효율적인 것 같아 단계별로 문제를 풀어가며 익혀가기로 마음먹었다. 따라서 발견한 곳이 HackerRank Practice의 Rerex파트이다.

하지만 아무런 개념이 없는 상태에서 이런 문제를 풀어내기에 막막했다.(찾는 글자를 그대로 입력하는 1번 문제 조차 풀지 못했다.) 따라서 우선 개념과 예제가 잘 정리되어 있는 ZVON.org 를 공부하기로 했다. 먼저 이번 포스트는 ZVON의 예제들을 정리해보자.

page 1

특정 텍스트를 찾고싶다. 어떡해야 할까? 그렇다 찾는 텍스트를 그대로 검색하면 된다.
source: Hello, world!
Regular Expression: Hello
Frist Match: Hello, world!
All Match: Hello, world!

Regular Expression: hello
Frist Match: Hello, world!
All Match: Hello, world!

page 3

글의 첫머리 또는 마지막의 단어를 찾고싶을 때,
글의 첫머리는 앞에 ^ 글의 마지막은 뒤에 $ 를 사용한다.

source: who is who
Regular Expression: ^who
Frist Match: who is who
All Match: who is who

Regular Expression: who$
Frist Match: who is who
All Match: who is who

page 4

특수 문자를 찾고 싶을 때 \를 붙인다.

Source: $12$\-\$25$

Regular Expression: ^$
Frist Match: $12$\-\$25$
All Match: $12$\-\$25$

Regular Expression: \$
Frist Match: $ 12$\-\$25$
All Match: $ 12 $ \-\$ 25 $

Regular Expression: ^\$
Frist Match: $ 12$\-\$25$
All Match: $ 12$\-\$25$

Regular Expression: \$$
Frist Match: $12$\-\$25\ $
All Match: $12$\-\$25\ $

Regular Expression: \\
Frist Match: 1212\-\$25$
All Match: 1212\-\$25$

page 5

. 는 모든 글자를 매칭시킨다.
단, 개행문자는 제외한다.
다음의 두 번째 케이스처럼 6글자 단위로 찾으라고 한다면 마지막 남은 5글자는 검색되지 않는다.

Source: Regular expressions are powerful!!!

Regular Expression: .
First match: Regular expressions are powerful!!!
All matches: Regular expressions are powerful!!!

Regular Expression: …
First match: Regular expressions are powerful!!!
All matches: Regular expressions are powerful!!!

page 7
"[]"안에 들어가는 단어들이 있으면 매칭시킨다.

출처: ZVON.org

댓글

이 블로그의 인기 게시물

CGV 상영시간표 알리

[Swift Error] Outlets cannot be connected to repeating content.

tensorflow softmax_cross_entropy_with_logits