1. 간단한 문자열 찾기


이번에는 평범한 텍스트를 이용해서 검색을 해보도록 하겠습니다. (평범한 텍스트도 정규 표현식이 될 수 있음) 아래를 한번 보도록 합시다.


Reg. Expression:

Text

Text:

ABCText Text ITexty

ioTextm Textll abcdety


그리고 한번, Text에 있는 "Text"의 'T'를 대문자가 아닌 소문자로 바꾸어 결과를 확인해봅시다. 대소문자를 구분하지 않고 text도, Text도 검색이 되는 것을 확인하실 수 있습니다. (VB6 같은 경우는 IgnoreCase라고 해서 대소문자 무시를 활성화 시킬수 있고, 비활성화 시키는 것도 할 수 있습니다. 다른 언어도 마찬가지로 존재합니다.)


이번에는, 그대로의 텍스트가 아닌 모르는 문자까지도 찾을 수 있는 방법을 알아보도록 합시다. 이 정규 표현식에서는 . 문자를 이용하면 쉽게 가능합니다. (. 문자는 아무런 문자 하나와 일치 합니다. 모든 문자를 가리킴)


Reg. Expression:

h.s

Text:

I was fifty years old to-day. Half a century has hurried by since I first lay in my mother's wondering arms. To be sure, I am not old; but I can no longer deceive myself into believing that I am still young. After all, the illusion of youth is a mental habit consciously encouraged to defy and face down the reality of age. If, at twenty, one feels that he has reached man's estate he, nevertheless, tests his strength and abilities, his early successes or failures, by the temporary and fictitious standards of youth.


여기서 보시면, . 문자를 이용하여 문자열을 검색하고 있습니다. 아까 말했듯이, . 문자는 아무런 문자 하나와 일치하다고 했었습니다. 검색된 부분을 살펴 보시면 has, his가 검색된 것을 확인하실 수 있습니다. 이번에는 h.s가 아닌 h..를 Reg. Expression에 작성하고 다시한번 결과를 살펴보도록 합시다.


Reg. Expression:

h..

Text:

I was fifty years old to-day. Half a century has hurried by since I first lay in my mother's wondering arms. To be sure, I am not old; but I can no longer deceive myself into believing that I am still young. After all, the illusion of youth is a mental habit consciously encouraged to defy and face down the reality of age. If, at twenty, one feels that he has reached man's estate he, nevertheless, tests his strength and abilities, his early successes or failures, by the temporary and fictitious standards of youth.


위와 같이 . 문자를 하나만이 아닌, 두개 이상을 사용해도 검색이 가능합니다. 그렇다면, '.' 문자 자체는 어떻게 검색을 할까요? 이 경우에는 역슬래시(\)를 사용하면 일반적인 문자의 검색이 가능합니다. 즉, \.는 마침표(.)와 같다는 말입니다. (참고로 역슬래시 같은 경우는 \를 두번 쓰면 평범한 문자인 역슬래시를 검색할 수 있습니다.)


Reg. Expression:

R\.

Text:

I think I should do well to name him, Mr. Alfred R. Phillips. It was not without its reward at the time.


Text를 보시면 r/R 뒤에 .가 등장해야만 검색이 된 것을 확인하실 수 있습니다. 반면에 ~s. 또는 ~e. 같은 경우는 검색이 되지 않았음을 확인하실 수 있습니다.


다음 강좌에서는 문자 집합에 대해 알아보도록 하겠습니다. 수고하셨습니다.