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
#QiitaBash MCPのセキュリティ
ryosukedtomita
0
610
生成AIコーディングとの向き合い方、AIと共創するという考え方 / How to deal with generative AI coding and the concept of co-creating with AI
seike460
PRO
1
350
Node-RED を(HTTP で)つなげる MCP サーバーを作ってみた
highu
0
120
GitHub Copilot and GitHub Codespaces Hands-on
ymd65536
1
130
今ならAmazon ECSのサービス間通信をどう選ぶか / Selection of ECS Interservice Communication 2025
tkikuc
20
3.8k
なぜ適用するか、移行して理解するClean Architecture 〜構造を超えて設計を継承する〜 / Why Apply, Migrate and Understand Clean Architecture - Inherit Design Beyond Structure
seike460
PRO
1
720
Code as Context 〜 1にコードで 2にリンタ 34がなくて 5にルール? 〜
yodakeisuke
0
120
PipeCDのプラグイン化で目指すところ
warashi
1
230
Team operations that are not burdened by SRE
kazatohiei
1
290
イベントストーミング図からコードへの変換手順 / Procedure for Converting Event Storming Diagrams to Code
nrslib
1
570
A2A プロトコルを試してみる
azukiazusa1
2
1.3k
来たるべき 8.0 に備えて React 19 新機能と React Router 固有機能の取捨選択とすり合わせを考える
oukayuka
2
880
Featured
See All Featured
[RailsConf 2023] Rails as a piece of cake
palkan
55
5.6k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Faster Mobile Websites
deanohume
307
31k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
46
9.6k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
34
5.9k
A Tale of Four Properties
chriscoyier
160
23k
Stop Working from a Prison Cell
hatefulcrawdad
270
20k
Adopting Sorbet at Scale
ufuk
77
9.4k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
8
800
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