regular expression in perl
Dreamweaver MX 2004의 Find and Replace 기능이나 여타의 검색엔진에서 perl 의 정규표현식을 빌려 쓰는 경우가 있다. perl 에 대해서는 전혀 모르나 쓸모가 있을 것 같아 인용한다. 정규 표현식을 사용하여 검색 옵션을 설정할 수 있는 경우가 많을 것 같다(추측이다 ㅡ ㅡ;).
원문의 작성자는 terzeron님이다.
1) 정규 표현식(regular expression)
다음은 regular expression에서 사용되는 기호들의 의미를 정리한 표이다. Perl에서의 pattern matching은 regular expression을 사용할 수 있으므로 확실하게 익혀두는 것이 바람직하다.
. newline을 제외한 임의의 한 글자
[a-z0-9] a-z까지의 영문자와 숫자 중의 한 글자
[^a-z0-9] a-z까지의 영문자와 숫자가 아닌 한 글자
\a Alarm, beep
\d 10진수 정수 한 글자, [0-9]
\D 10진수 정수가 아닌 한 글자, [^0-9]
\w 영문자 또는 숫자, _(underline) 중의 한 글자, [a-zA-Z0-9_]
\W \w가 아닌 한 글자, [^a-zA-Z0-9_]
\s 공백문자(whitespace) 한 글자 (space, tab, newline …)
\S \s가 아닌 한 글자
\n newline
\r carriage return
\t tab
\f formfeed
\b []내부에서 사용될 경우 backspace
\0, \000 null character
\nnn nnn이라는 8진수의 값을 가지는 ASCII 한 글자
\xnn nn이라는 16진수의 값을 가지는 ASCII 한 글자
\cX Ctrl+X에 해당하는 한 글자
\metachar \|, \*, \\, \(, \), \[, \{, \^, \$, \+, \?, \.와 같이 표현되며 |, *, \, (, ), [, {, ^, $, +, ?, .의 의미를 가지는 한 글자
(abc) 뒤에 가서 참조될(backreference) abc라는 문자열
\1 첫 번째 참조 문자열, open parenthesis, '('에 의해 순서가 결정됨
\2 두 번째 참조 문자열
\n n번째 참조 문자열, 10이상일 경우 backreference될 문자열이 없으면, 8진수 값의 ASCII 한 글자의 의미를 가질 수 있다.
x? x가 나오지 않거나 1번 나오는 경우
x* x가 0번 이상 반복되는 문자열
x+ x가 1번 이상 반복되는 문자열
x{m,n} x가 m번 이상, n번 미만으로 나오는 문자열
x{m,} x가 m번 이상 나오는 문자열
x{m} x가 m번 나오는 문자열
abc a와 b와 c가 연속으로 붙어서 나오는 문자열
abc|def|ghi 문자열 abc, def, ghi 중의 하나에 해당되는 경우
다음은 크기가 없는, 지정된 위치가 일치하도록 제시할 수 있는 위치 표시 조건(assertion)들을 나열한 표이다.
\b []바깥에서 \w와 \W가 서로 바뀌고 있는 word boundary를 의미함
\B \b가 아닌 경우
\A string의 처음 위치
\Z string의 마지막 위치
\G 마지막으로 m//g에 의해 match가 된 위치
^ string의 가장 처음 위치 표시로 /m modifier가 있으면 line의 처음을 의미한다.
$ string의 가장 마지막 위치 표시로 /m modifier가 있으면 line의 끝을 의미한다.
(?=…) …위치에 놓일 문자열이 매치 된다는 조건
(?!…) …위치에 놓일 문자열이 매치 되지 않는다는 조건
다음 예문을 가지고 regular expression으로 pattern matching을 하는 간단한 예를 선보이기로 한다. 예문은 각 줄 끝마다 newline이 붙어있는 것으로 가정한다.
In France a man who has ruined himself for women is generally regarded with sympathy and admiration; there is a feeling that it was worth while, and the man who has done it feels even a certain pride in the fact; in England he will be thought and will think himself a damned fool. That is why Antony and Cleopatra has always been the least popular of Shakespeare’s greater plays.
/France/; # 1번째 줄
/(women|there|That) is/; # 1, 2, 4번째 줄
/\we{2}\w/; # feel과 been, 3, 5번째 줄
/is$/; # 4번째 줄
/^England/; # 4번째 줄
if (/Engl(\w+)/) {
print "Engl$1"; # English, England
}
print if /the/; # there, the, the, 2, 3, 5번째 줄
print if /\bthe\b/; # the, the, 3, 5번째 줄
print if /man|women/; # man, man, 1, 3번째 줄
if ( /(feel(s|ing))/ ) {
print $1; # feeling, feels, 2, 3번째 줄
}
if (/([A-Z]([a-z][a-z]+))/) {
print $2; # rance, ngland, ntony
}
주목할 것은 /man|women/에서 women이라는 단어가 단락 내에 존재함에도 불구하고 match가 일어나지는 않는다는 것이다. 자세한 이유는 modifier에 관한 설명을 참조하도록 하자. 다음은 단순한 기호들의 나열 같아서 약간 더 어려울 것 같은 예제를 골라보았다.
(0|0x)\d*\s\1\d*
# 0×1234 0×4321같은 16진수 두 개에서 match된다.
/.*foo/
# foo로 끝나는 단어에서 match된다.
/^(\d+\.?\d*|\.\d+)$/;
# 올바른 소수표현에서 match가 일어난다.
다음은 the를 관사로 가지는 명사들을 모두 출력하는 예제 프로그램이다.
while (<>) {
if (/\bthe\b\s+(\w+)/g) {
print "$1\n";
}
}
About this entry
You’re currently reading “regular expression in perl,” an entry on multiplicité
- Published:
- Thursday, July 29th, 2004 at 4:54 am
- This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.0 Korea.
- Author:
- vizualizer
- Category:
- Server Side
Similar Posts
- xml 로딩 시 각 노드의 변수형
- common sub-expression elimination
- escape()
- HTML 제거 메써드
- What’s new in action script v2_Boolean() Class
- print special characters in random
- www.asanokohei.com
- hologram in action
- spatial element in video game; especially in shadow of the colossus
- What’s new in action script v2_Array.sortOn() method
No comments
Jump to comment form | comments rss