1 / 20

Regular Expression

Regular Expression. Veloxsoft Member Education 2005-04-08 작성 , 발표 : 개발 5 팀 김기용. Introduction. 일반적인 문자열 비교 함수들 C : strcmp(s1, s2) Java : s1.equals(s2) JavaScript : (s1 == s2) Perl : (s1 eq s2) VB : (s1 = s2) 확장된 문자열 비교 / 검색 ( 겨우 이정도 ?) Ignore case: [C] strcmpi

samson
Download Presentation

Regular Expression

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Regular Expression Veloxsoft Member Education 2005-04-08 작성, 발표: 개발 5팀 김기용

  2. Introduction • 일반적인 문자열 비교 함수들 • C : strcmp(s1, s2) • Java : s1.equals(s2) • JavaScript : (s1 == s2) • Perl : (s1 eq s2) • VB : (s1 = s2) • 확장된 문자열 비교/검색 (겨우 이정도?) • Ignore case: [C] strcmpi • Inclusion: [C] strchr, strstr • 좀 더 다양한 문자열 검색/치환 필요

  3. Where to use • 전통적인 unix commands • grep / sed / awk • Text editors • vi / emacs / MSDEV / EditPlus / UltraEdit / … • Languages • JavaScript / Perl / Python / VB.net / Java / … • Libraries • CRegExp in MFC … • PCRE in many languages

  4. 기본 문법 • 대개 / (slash) 로 시작하여 / 로 끝남. • 두 / 사이에 찾고자 하는 문자열을 넣으면 간단한 정규식이 만들어진다. • 아래 정규식은 velox 라는 문자열을 찾는다.

  5. Wildcard • . : Match any character /bb.om/ =~ “bbbom”, “bbcom”, “bbdom”, … • [] : Character class /bb[abc]om/ =~ "bbaom", "bbbom", "bbcom", … !~ "bbdom", "bb.om", … /bb[a-z]om/ =~ "bbaom", "bbbom", … , "bbzom“ !~ "bbAom", "bb.om", "bb!om", … /bb[a-ckA-C0-8]om/ =~ "bbaom", "bbkom", "bbAom", "bb7om", … !~ "bbdom", "bbDom", "bb9om", … /bb[^ag-k]om/ =~ "bbbom", "bbcom", … !~ "bbaom", "bbgom", …, "bbkom"

  6. Perl / JavaScript 에서 가장 잘 지원 PHP 에서도 PCRE (Perl Compatible Regular Expression) 을 사용할 수 있음 Tool 마다 지원하는 Set 가 다름 Escaped wildcard

  7. Quantifier • {min, max} /bbc{2}om/ =~ "bbccom" !~ "bbcbbcom" /bb[abc]{2}om/ =~ "bbabom", "bbbbom", “bbcaom” !~ "bbcom", “bbabcom” /bb[a-z]{3,7}om/ =~ "bbcomom", "bbcomcomom", “bbscgyongom” !~ "bbabom", "bbbluekissom"

  8. Short Quantifier /bbc?om/ =~ “bbom”, “bbcom” /bbc+om/ =~ “bbcom”, “bbccom”, “bbcccom”, … !~ “bbom” /bbc*om/ =~ “bbom”, “bbcom”, “bbccom”, …

  9. Grouping • () : group /b(bco)*m/ =~ "bm", "bbcom", “bbcobcom” /bb([a-z]o)+m/ =~ "bbcom", "bbzom", “bbaobocodoeom” !~ "bbm", "bbazm"

  10. Boundary • ^ : Match the beginning of the line • $ : Match the end of the line /bbcom/ =~ “my bbcomm” /^bbcom/ =~ “bbcomm” !~ “my bbcomm” /bbcom$/ =~ “my bbcom” !~ “bbcomm” /\bbbcom\b/ =~ “bbcom good” !~ “bbcomm”

  11. Selection • Using vertical bar /bb(com|micro)soft/ =~ "bbcomsoft", “bbmicrosoft” !~ "bbsoft"

  12. Substitution • Format /search pattern/replacement text/ • perl $str = "The microsoft conquers the market"; $str =~ s/micro/velox/; • sed # cat text.txt The microsoft conquers the market # cat text.txt | sed 's/microsoft/veloxsoft/' The veloxsoft conquers the market #

  13. Back reference Example

  14. Maximal / Minimal matching • Greedy str = "bbcommunication"; $str =~ s/bbcom*/babo/; # str becomes: "babounication"; • Which * ? $str = "bbcommunication"; $str =~ s/(bbcom*)(m*uni)/\1_\2/; # str -> "bbcomm_unication" • Minimal $str = "bbcommunication"; $str =~ s/(bbcom*?)(m*uni)/\1_\2/; # str -> "bbco_mmunication"

  15. Search Options • i : ignore case • g : global search /bbcom/i =~ "BbCom " $str = "BbCom"; $str =~ s/b/Bee/gi; # str -> "BeeBeeCom“

  16. MSDEV Replace Example • Find: ^\([0-9A-Za-z\.]+\) \(.+\)$ • Replace: <TR><TH><A HREF="http://\1">\1</A></TH><TD>\2</TD>

  17. 응용예 1 • Replace in Files (bash & sed) $ for file in *.h *.cpp; do sed 's/TCmdWindow/TFrame/g' < $file > tmp; mv tmp $file; done • 텍스트  HTML 포매팅 • 문서 coloring (용문객잔 / PERL) • http://scgyong.new21.net/study/misc/yongmun/ • 주소록 txt  HTML table (MSDEV)

  18. 응용예 2 – 로그 파일 분석 • Log 에 현재 시각을 출력하는 루틴 작성 class Debug { public static void logTimeStamp(String str) { System.out.println("<" + str + “:" + System.currentTimeMillis() + “>"); } } • 프로그램에서 특정 키워드와 함께 시각을 출력 class AEmergencyCarlet extends Carlet { public void start() { Debug.logTimeStamp("Carlet.Acn.Start"); dial(); } public void connected() { Debug.logTimeStamp("Carlet.Acn.Connected"); } } • Log 분석기에서 특정 키워드의 시각을 출력하거나 키워드 사이 값을 계산 next unless (($key, $time) = ($l =~ /^<(\S+):(\d+)>\s*$/));

  19. 응용예 3 – 만화 다운 받기 #!/usr/bin/perl $wget = '/usr/bin/wget'; $rfmt = 'http://ntamin.paran.com/webToon/web_toon/toon_view.asp?page=%d&cartoon_num=1'; $lfmt = 'page_%03d.html'; for ($page = 1; ; $page++) { $url = sprintf($rfmt, $page); $loc = sprintf($lfmt, $page); $cmd = "$wget -c -O $loc '$url'"; print "$cmd\n"; `$cmd`; last if ($? == 256); checkFile(); } sub checkFile { open(FILE, $loc) or die $!; while (<FILE>) { if (/^\s*<option value="(\d+)" selected >\d+\1<\/option>\s*$/) { exit if ($page != $1); } if (/^\s*<td>등록일\s*:\s*(\d{4}\.\d{2}\.\d{2})<\/td>\s*$/) { $date = $1; $date =~ s/\./_/g; } if (/^\s*<td\s+align="center"><img\s+src=".+UPLOAD\/Cartoon\/([^"]+)"\s*border="0"/) { $file = $1; downloadFile(); } } close(FILE); } sub downloadFile { $remote = "http://ntafile.paran.com/UPLOAD/Cartoon/$file"; $local = sprintf('1001_%03d_%s.jpg', $page, $date); $cmd = "$wget -c -O $local $remote"; print"$cmd\n"; `$cmd`; last if ($? == 256); }

  20. Reference • The C Programming Language • PerlDOC Regular Expression $ perldoc perlre $ perldoc –T perlre | less -r • http://scgyong.new21.net/study/re/

More Related