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
時間軸から考えるTerraformを使う理由と留意点
fufuhu
16
4.8k
今だからこそ入門する Server-Sent Events (SSE)
nearme_tech
PRO
3
240
OSS開発者という働き方
andpad
5
1.7k
Testing Trophyは叫ばない
toms74209200
0
880
Cache Me If You Can
ryunen344
2
1.5k
MCPとデザインシステムに立脚したデザインと実装の融合
yukukotani
4
1.4k
「待たせ上手」なスケルトンスクリーン、 そのUXの裏側
teamlab
PRO
0
550
Improving my own Ruby thereafter
sisshiki1969
1
160
モバイルアプリからWebへの横展開を加速した話_Claude_Code_実践術.pdf
kazuyasakamoto
0
330
ぬるぬる動かせ! Riveでアニメーション実装🐾
kno3a87
1
230
AI Coding Agentのセキュリティリスク:PRの自己承認とメルカリの対策
s3h
0
230
詳解!defer panic recover のしくみ / Understanding defer, panic, and recover
convto
0
240
Featured
See All Featured
Typedesign – Prime Four
hannesfritz
42
2.8k
Embracing the Ebb and Flow
colly
87
4.8k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Producing Creativity
orderedlist
PRO
347
40k
GraphQLとの向き合い方2022年版
quramy
49
14k
jQuery: Nuts, Bolts and Bling
dougneiner
64
7.9k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Designing for Performance
lara
610
69k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.6k
BBQ
matthewcrist
89
9.8k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
8
530
Context Engineering - Making Every Token Count
addyosmani
3
50
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