Download presentation
Presentation is loading. Please wait.
Published bySara Bryan Modified over 9 years ago
1
Embedded System Expert Regular Expression – Veloxsoft Member Education 1 / 20 Regular Expression Veloxsoft Member Education 2005-04-08 작성, 발표 : 개발 5 팀 김기용
2
Embedded System Expert Regular Expression – Veloxsoft Member Education 2 / 20 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
Embedded System Expert Regular Expression – Veloxsoft Member Education 3 / 20 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
Embedded System Expert Regular Expression – Veloxsoft Member Education 4 / 20 기본 문법 대개 / (slash) 로 시작하여 / 로 끝남. 두 / 사이에 찾고자 하는 문자열을 넣으면 간단한 정규식이 만들어진다. 아래 정규식은 velox 라는 문자열을 찾는다. Perl: $str = "my veloxsoft"; if ($str =~ /velox/) { print “Found\n"; } JavaScript: var str = "my veloxsoft"; if (str.match(/velox/)) { out.innerText = “Found\n"; } MFC with CRegExp: CString str = "my veloxsoft"; CRegExp re = “velox"; if (re.Match(str)) { AfxMessageBox(“Found"); } MSDEV:
5
Embedded System Expert Regular Expression – Veloxsoft Member Education 5 / 20 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
Embedded System Expert Regular Expression – Veloxsoft Member Education 6 / 20 Escaped wildcard 이름단축명령내용 Whitespace\s[ \t\n\r\f] Word character\w[a-zA-Z0-9] Digit\d[0-9] Non-whitespace\S[^ \t\n\r\f] Non-word character\W[^a-zA-Z0-9] Non-digit\D[^0-9] Perl / JavaScript 에서 가장 잘 지원 PHP 에서도 PCRE (Perl Compatible Regular Expression) 을 사용할 수 있음 Tool 마다 지원하는 Set 가 다름
7
Embedded System Expert Regular Expression – Veloxsoft Member Education 7 / 20 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
Embedded System Expert Regular Expression – Veloxsoft Member Education 8 / 20 Short Quantifier 의미단축내용 zero or one?{0,1} zero or more*{0,} one or more+{1,} /bbc?om/ =~ “bbom”, “bbcom” /bbc+om/ =~ “bbcom”, “bbccom”, “bbcccom”, … !~ “bbom” /bbc*om/ =~ “bbom”, “bbcom”, “bbccom”, …
9
Embedded System Expert Regular Expression – Veloxsoft Member Education 9 / 20 Grouping () : group /b(bco)*m/ =~ "bm", "bbcom", “bbcobcom” /bb([a-z]o)+m/ =~ "bbcom", "bbzom", “bbaobocodoeom” !~ "bbm", "bbazm"
10
Embedded System Expert Regular Expression – Veloxsoft Member Education 10 / 20 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
Embedded System Expert Regular Expression – Veloxsoft Member Education 11 / 20 Selection Using vertical bar /bb(com|micro)soft/ =~ "bbcomsoft", “bbmicrosoft” !~ "bbsoft"
12
Embedded System Expert Regular Expression – Veloxsoft Member Education 12 / 20 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
Embedded System Expert Regular Expression – Veloxsoft Member Education 13 / 20 Back reference Example In Replacement $str = "first last"; $str =~ s/^(\S+)\s+(\S+)$/\2 \1/; In pattern /(.+)\1/ =~ "bbcom", “gesture of offence" JavaScript var re = /(..):(..):(..)/; var str = "12:34:56"; var arr = str.match(re); if (arr) { var hours = arr[1]; var minutes = arr[2]; var seconds = arr[3]; } PERL $time = "12:34:56"; if ($time =~ /(..):(..):(..)/) { $hours = $1; $minutes = $2; $seconds = $3; } CRegExp (MFC) CRegExp re = "(..):(..):(..)"; CString str = "12:34:56"; if (re.Match(str)) { CString hours = re[1]; CString minutes = re[2]; CString seconds = re[3]; }
14
Embedded System Expert Regular Expression – Veloxsoft Member Education 14 / 20 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
Embedded System Expert Regular Expression – Veloxsoft Member Education 15 / 20 Search Options i : ignore case g : global search /bbcom/i =~ "BbCom " $str = "BbCom"; $str =~ s/b/Bee/gi; # str -> "BeeBeeCom“
16
Embedded System Expert Regular Expression – Veloxsoft Member Education 16 / 20 MSDEV Replace Example Find: ^\([0-9A-Za-z\.]+\) \(.+\)$ Replace: \1 \2
17
Embedded System Expert Regular Expression – Veloxsoft Member Education 17 / 20 응용예 1 Replace in Files (bash & sed) $ for file in *.h *.cpp; do sed 's/TCmdWindow/TFrame/g' tmp; mv tmp $file; done 텍스트 HTML 포매팅 – 문서 coloring ( 용문객잔 / PERL) http://scgyong.new21.net/study/misc/yongmun/ – 주소록 txt HTML table (MSDEV)
18
Embedded System Expert Regular Expression – Veloxsoft Member Education 18 / 20 응용예 2 – 로그 파일 분석 Log 에 현재 시각을 출력하는 루틴 작성 class Debug { public static void logTimeStamp(String str) { System.out.println(" "); } 프로그램에서 특정 키워드와 함께 시각을 출력 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*$/));
19
Embedded System Expert Regular Expression – Veloxsoft Member Education 19 / 20 응용예 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 ( ) { if (/^\s* \d+\1 \s*$/) { exit if ($page != $1); } if (/^\s* 등록일 \s*:\s*(\d{4}\.\d{2}\.\d{2}) \s*$/) { $date = $1; $date =~ s/\./_/g; } if (/^\s* <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
Embedded System Expert Regular Expression – Veloxsoft Member Education 20 / 20 Reference The C Programming Language PerlDOC Regular Expression $ perldoc perlre $ perldoc –T perlre | less -r http://scgyong.new21.net/study/re/
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.