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
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
350
開発生産性を上げるための生成AI活用術
starfish719
3
1.5k
20251016_Rails News ~Rails 8.1の足音を聴く~
morimorihoge
2
620
Devoxx BE 2025 Loom lab
josepaumard
0
110
ソフトウェア設計の実践的な考え方
masuda220
PRO
4
620
CSC509 Lecture 05
javiergs
PRO
0
310
『毎日の移動』を支えるGoバックエンド内製開発
yutautsugi
2
270
アメ車でサンノゼを走ってきたよ!
s_shimotori
0
230
タスクの特性や不確実性に応じた最適な作業スタイルの選択(ペアプロ・モブプロ・ソロプロ)と実践 / Optimal Work Style Selection: Pair, Mob, or Solo Programming.
honyanya
3
180
フロントエンド開発のためのブラウザ組み込みAI入門
masashi
6
3.2k
Six and a half ridiculous things to do with Quarkus
hollycummins
0
190
はじめてのDSPy - 言語モデルを『プロンプト』ではなく『プログラミング』するための仕組み
masahiro_nishimi
3
930
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
332
24k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.5k
How STYLIGHT went responsive
nonsquared
100
5.8k
Why You Should Never Use an ORM
jnunemaker
PRO
59
9.6k
Building a Modern Day E-commerce SEO Strategy
aleyda
44
7.8k
Rails Girls Zürich Keynote
gr2m
95
14k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.4k
YesSQL, Process and Tooling at Scale
rocio
173
14k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
115
20k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.1k
GraphQLの誤解/rethinking-graphql
sonatard
73
11k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
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