Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
introduction to Regular Expression
Search
CCN
May 13, 2013
Programming
0
41
introduction to Regular Expression
2013/5/13 in STUST CSIE
CCN
May 13, 2013
Tweet
Share
Other Decks in Programming
See All in Programming
DMMプラットフォームがTiDB Cloudを採用した背景
pospome
9
4.2k
Milestoner
bkuhlmann
1
410
MicrosoftのPlatform Engineeringガイドを読んで実際になにかやってみた
ymd65536
1
490
try! Swift Tokyo 2024 参加報告 / try! Swift Tokyo 2024 Report
hironytic
0
210
Ruby Function Composition
bkuhlmann
1
340
Next.js App Router
quramy
11
1.6k
Scalable Customer Journey Orchestration (CJO)
lewuathe
0
410
PHPの次期バージョンはこの時期どうなっているのか - Internalsの開発体制について - PHPカンファレンス小田原
youkidearitai
PRO
1
210
Try creating your own orderedmap
kazamori
1
170
Komplexe Oberflächen mit SVG und der Web Animation API
joergneumann
0
680
CREってこういうこと? 体験入社 - 提案資料 - / what-is-cre-trial-employment
shinden
1
460
PHPはいつから死んでいるかの調査
chiroruxx
2
410
Featured
See All Featured
Automating Front-end Workflow
addyosmani
1357
200k
Clear Off the Table
cherdarchuk
85
310k
How STYLIGHT went responsive
nonsquared
92
4.8k
Art, The Web, and Tiny UX
lynnandtonic
290
19k
How to name files
jennybc
65
93k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
19
1.6k
Pencils Down: Stop Designing & Start Developing
hursman
117
11k
KATA
mclloyd
16
12k
Producing Creativity
orderedlist
PRO
338
39k
Imperfection Machines: The Place of Print at Facebook
scottboms
261
12k
YesSQL, Process and Tooling at Scale
rocio
165
13k
Stop Working from a Prison Cell
hatefulcrawdad
267
19k
Transcript
introduction to Regular Expression CCN KKBOX, Inc. 2013/5/13 STUST
什麼是 Regular Expression
什麼是 Regular Expression • 描述某個語法規則的字串 (Pattern) • 強⼤大的⽂文字處理⼯工具
就算你不寫程式
你還是有機會⽤用到
什麼是 Regular Expression • 處理⼤大量⽂文字 • 統計 • 分析 •
報表
它可以做什麼 • 搜尋字串 (match) • 取代字串 (replace)
Pattern Match
電話號碼 • 0988-123456 • 09\d\d-?\d\d\d-?\d\d\d • 09\d{2}-?\d{3}-?\d{3} • 06-253-3131 •
0\d-?\d{3,4}-?\d{4}
IP Address • (25[0-5]|2[0-4][0-9]|1?[0-9]{1,2}\.){3} 25[0-5]|2[0-4][0-9]|1?[0-9]{1,2} • 127.0.0.1 • 168.95.1.1 •
256.0.3.1
⾝身分證字號 • [A-Z][12]\d{8} • A123456789 • B222555888
Link • <a href=”http://tw.kkbox.com”>KKBOX</a> • /href=[‘“](.*?)[‘“]/ • http://tw.kkbox.com • /href=[‘“](.*?)[‘“]>([^<]+)<\/a>/
• http://tw.kkbox.com • KKBOX
Pattern Replace
移除 HTML Tag • <h1>Section1</h1><p>hello world</p> • s/<[^>]+>/ / •
Section hello world
移除 HTML Tag ‧全校活動⼀一覽表 ‧南臺校訊 ‧圖書館 ! ! ‧藝⽂文中⼼心 ‧UCAN職能診斷平台
‧南臺專利技轉、讓與公告 ! ! ‧本校⽤用電資料 ‧海外留學資訊 ‧雲嘉南區域教學資源中⼼心 ! ! ‧註冊選課專區 ‧推廣教育招⽣生 ‧南臺志⼯工⼤大學 ! ! ‧南臺幼稚園 ‧考選部 ‧南臺Facebook粉絲團 ! ! ‧社團活動專區 ‧性別平等教育委員會 ‧南臺科⼤大夏令營
移除空⽩白⾏行 • s/^\s*$// ‧全校活動⼀一覽表 ‧南臺校訊 ‧圖書館 ‧藝⽂文中⼼心 ‧UCAN職能診斷平台 ‧南臺專利技轉、讓與公告 ‧本校⽤用電資料
‧海外留學資訊 ‧雲嘉南區域教學資源中⼼心 ‧註冊選課專區 ‧推廣教育招⽣生 ‧南臺志⼯工⼤大學 ‧南臺幼稚園 ‧考選部 ‧南臺Facebook粉絲團 ‧社團活動專區 ‧性別平等教育委員會 ‧南臺科⼤大夏令營
基本語法
Characters (字元)
Character sequence • /foo/ - match foo • /bar/ -
match bar • . - wildcard (萬⽤用字元)
Character class • denoted by [] • any character sequence
• [abc], [AaBbCc], [012345] • character range • [0-9], [A-Z], [0-9A-Z] • mix • [ABC0-5], [A-F0-9]
Character class • Inverse denoted by [^] • inverse sequence
• [^abc], [^AaBbCc], [^012345] • inverse range • [^0-9], [^A-Z], [^0-9A-Z]
Character class • example • /m[ao]de/ • match - made
or mode • /[Ss]tust/ • match - Stust or stust
Predefined character classes from Wikipedia
Quantifiers (數量)
+ • match 1+ times • /a+b/ • match ab
• match aab • match aaab • not match b
* • match 0+ times • /a*b/ • match ab
• match aab • match aaab • also match b
? • match 0 or 1 times • /a?b/ •
match ab • match b
{n, m} • match least n time but no more
than m times • /a{2, 3}b/ • match aab • match aaab • not match ab • not match aaaab
{n} • match n time • /a{2}b/ • match aab
• not match ab • not match aaab
Characters && Quantifiers • 0\d-?\d{3,4}-?\d{4} • 06-253-3131 • 02-8825252 •
028825252 • 037-000-2222 • Tel: 110
Characters && Quantifiers • [A-Z][12]\d{8} • A123456789 • AA123456789 •
A369000111 • a123456789
Assertions (斷點)
^ • ⾏行頭 (beginning of line) • ^[A-Z][12]\d{8} • A123456789
• AA123456789 • A1234567890
$ • ⾏行尾 (ending of line) • [A-Z][12]\d{8}$ • A123456789
• AA123456789 • A1234567890
^ && $ • ⾏行尾 (ending of line) • ^[A-Z][12]\d{8}$
• A123456789 • AA123456789 • A1234567890
\b • 字的邊界 (word boundary) • \b[A-Z][12]\d{8}\b • hello A123456789
world • BBA123456789123 • A1234567890
Captures
⽤用括號來表⽰示⼀一個 Group • (\w)(\w)\2\1 • AAAA • ABBA • ABCD
• CDCD • ABAB
Greediness
Greedy • <tr><td>A</td><td>B</td></tr> • /<td>.*<\/td>/ • <td>A</td><td>B</td>
Non-greedy • <tr><td>A</td><td>B</td></tr> • /<td>.*?<\/td>/ or /<td>.*<\/td>/U • <td>A</td> •
<td>B</td>
基本語法 • Characters (字元) • Quantifiers (數量) • Assertions (斷點)
• Captures • Greediness
可以幹⿇麻?
南台⾸首⾴頁公告 • http://news.stust.edu.tw/classid/ ! ! ! !
⼗十分鐘寫出正妹牆?
PCHome 24H 降價通知 ! ! ! ! !
程式碼搜尋
access log 分析 • 搭配各項⼯工具 • grep • awk •
sed • sort • uniq
QA