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
ペアプロ × 生成AI 現場での実践と課題について / generative-ai-in-pair-programming
codmoninc
2
20k
VS Code Update for GitHub Copilot
74th
2
660
テスト駆動Kaggle
isax1015
1
480
iOS 26にアップデートすると実機でのHot Reloadができない?
umigishiaoi
0
130
20250628_非エンジニアがバイブコーディングしてみた
ponponmikankan
0
710
#kanrk08 / 公開版 PicoRubyとマイコンでの自作トレーニング計測装置を用いたワークアウトの理想と現実
bash0c7
1
880
Modern Angular with Signals and Signal Store:New Rules for Your Architecture @enterJS Advanced Angular Day 2025
manfredsteyer
PRO
0
230
AI コーディングエージェントの時代へ:JetBrains が描く開発の未来
masaruhr
1
200
20250704_教育事業におけるアジャイルなデータ基盤構築
hanon52_
5
880
MCPを使ってイベントソーシングのAIコーディングを効率化する / Streamlining Event Sourcing AI Coding with MCP
tomohisa
0
150
Result型で“失敗”を型にするPHPコードの書き方
kajitack
5
970
脱Riverpod?fqueryで考える、TanStack Queryライクなアーキテクチャの可能性
ostk0069
0
320
Featured
See All Featured
Git: the NoSQL Database
bkeepers
PRO
430
65k
Reflections from 52 weeks, 52 projects
jeffersonlam
351
21k
Why You Should Never Use an ORM
jnunemaker
PRO
58
9.4k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
107
19k
Balancing Empowerment & Direction
lara
1
440
StorybookのUI Testing Handbookを読んだ
zakiyama
30
5.9k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
126
53k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.8k
The Cost Of JavaScript in 2023
addyosmani
51
8.5k
A Modern Web Designer's Workflow
chriscoyier
695
190k
GraphQLの誤解/rethinking-graphql
sonatard
71
11k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
48
2.9k
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