Presentation is loading. Please wait.

Presentation is loading. Please wait.

Regular Expression 1 Powerful pattern matching with regular expression to a string while (<>) { if ( /ab*c/ ) { print $_; } } substitute operator s/abc*c/def/;

Similar presentations


Presentation on theme: "Regular Expression 1 Powerful pattern matching with regular expression to a string while (<>) { if ( /ab*c/ ) { print $_; } } substitute operator s/abc*c/def/;"— Presentation transcript:

1 Regular Expression 1 Powerful pattern matching with regular expression to a string while (<>) { if ( /ab*c/ ) { print $_; } } substitute operator s/abc*c/def/; /pattern/

2 Regular Expression 2 Regular Expression 은 Pattern을 설명한다
/a./ : a로 시작하고 두개 문자로 된 문자열 /[abcde]/ : 다섯개 문자중 어느거나 /[aeiouAEIOU]/ : 모든 영문자 모음과 매칭 /[ ]/ 혹은 /[0-9]/ /[^0-9]/ : 숫자가 아닌 어떤 문자와 매칭

3 Regular Expression 3 간편하게 표시하는 문자 하나 짜리 패턴 \d (a digit): [0-9]
\w (word char): [a-zA-Z0-9_] \W : [^a-zA-Z0-9_] \s (space): [ \r\t\n\f] \S : [^ \r\t\n\f]

4 Regular Expression 4 Grouping Pattern(패턴 그룹) Sequence : 순서대로 나열
Abc Multiplier : 바로 앞 패턴의 반복 * : 0번 이상 반복 (0번도 O.K.) + : 1번 이상 반복 ? : 0혹은 1번 예 ) /fo+ba?r/는 어떤 문자열과 매칭이 될까? /x{5,10}/ : 최소 5개의 x에서 10개의 x와 매칭

5 Regular Expression 5 Grouping Pattern(패턴 그룹) 괄호를 이용한 기억
/a(.*)b\1c/ : \1은 첫번째 괄호에 해당되는 패턴 aFREDbFREDc 혹은 abc는 ?, aXXbXXXc 는 ? s/a(.*)b(.*)c/$1B$2/ Alternation(선택) /a|b|c/ 혹은 /song|blue/

6 Regular Expression 6 Anchoring Pattern(패턴 고정하기) \b :단어의 시작을 지정
/fred\b/ : fred는 성공 frederick은 실패 /\bmo/ : mo와 moe는 성공 Elmo는 실패 /^a/ : 문자열의 첫글자가 a에 매칭하면 성공 /a^/ : 는 ^의 특수한 의미를 상실함…. /a$/ : 문자열의 마지막 글자가 a에 매칭하면 성공 패턴 중간에 $ 문자를 사용하려면 \$를 쓰도록

7 Regular Expression 7 Precedence(우선 순위) 만약 a|b*라는 패턴은 ?
Regular Expression Operator의 우선순위 1등 : 괄호 ( ) 2등 : 반복(*) ? + * {m,n} 3등 : sequence와 anchoring : abc ^ $ 마지막 : 선택 ( | ) abc*, (abc)*, ^x|y, ^(x|y), a|bc|d

8 Target($_)을 바꾸는 방법 If pattern to search is not in $_, use =~
$a = “hello world”; $a =~ /^he/; $a =~ /$b/i i 는 소문자 대문자 구별을 무시하도록 함

9 Delimiter를 바꾸는 방법 if ( <> =~ /\wwwroot\/docs/) { …}
m#/wwwroot/doc/#

10 패턴에서 변수 사용 $what = “bird”; $sentence = “Every good bird does fly.”;
if ( $sentence =~ /\b$what\b/ ) { print “The sentence has the word $what!\n”; }

11 문자열 변경 s/old-regex/new-string/ 예1) 예2) 예3) $_ =“foot fool buffoon”;
s/foo/bar/g; # $_ 가 “bart barl bufbarn”로 변경 예2) $old = “hello world”; $change = “goodbye”; $old =~ s/hello/$change/; 예3) $_ =“this is a test”; s/ (\w+)/<$1>/g; # $_ 는 “<this> <is> <a> <test>”로 바꿤


Download ppt "Regular Expression 1 Powerful pattern matching with regular expression to a string while (<>) { if ( /ab*c/ ) { print $_; } } substitute operator s/abc*c/def/;"

Similar presentations


Ads by Google