$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Regular Expressions
Search
John Chandler
November 22, 2012
Programming
0
15
Regular Expressions
Internal presentation on regular expressions, 2012
John Chandler
November 22, 2012
Tweet
Share
More Decks by John Chandler
See All by John Chandler
Everything I've Learnt from Russel Winder's Talks, in 5 Minutes (A Tribute)
metaljoe
0
12
Snowboarding Saved My Software Career
metaljoe
0
10
Neurodiversity in Tech
metaljoe
0
55
Neurodiversity Lightning Talk
metaljoe
0
13
Tackling a Legacy Codebase with (Micro)Services
metaljoe
0
11
Testing Workshop - Part 1 (PyConUK 2011)
metaljoe
0
19
Testing Workshop - Part 2 (PyConUK 2011)
metaljoe
0
14
Python, DOT, and Visualising Legacy Code The Lazy Way
metaljoe
0
20
Test Driven Development (A Short Introduction)
metaljoe
0
12
Other Decks in Programming
See All in Programming
テストやOSS開発に役立つSetup PHP Action
matsuo_atsushi
0
150
ローターアクトEクラブ アメリカンナイト:川端 柚菜 氏(Japan O.K. ローターアクトEクラブ 会長):2720 Japan O.K. ロータリーEクラブ2025年12月1日卓話
2720japanoke
0
730
DSPy Meetup Tokyo #1 - はじめてのDSPy
masahiro_nishimi
1
160
エディターってAIで操作できるんだぜ
kis9a
0
710
Full-Cycle Reactivity in Angular: SignalStore mit Signal Forms und Resources
manfredsteyer
PRO
0
210
Socio-Technical Evolution: Growing an Architecture and Its Organization for Fast Flow
cer
PRO
0
320
手が足りない!兼業データエンジニアに必要だったアーキテクチャと立ち回り
zinkosuke
0
630
ViewファーストなRailsアプリ開発のたのしさ
sugiwe
0
440
dnx で実行できるコマンド、作ってみました
tomohisa
0
150
関数実行の裏側では何が起きているのか?
minop1205
1
690
堅牢なフロントエンドテスト基盤を構築するために行った取り組み
shogo4131
8
2.3k
AIコーディングエージェント(Gemini)
kondai24
0
210
Featured
See All Featured
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Become a Pro
speakerdeck
PRO
31
5.7k
Code Reviewing Like a Champion
maltzj
527
40k
A designer walks into a library…
pauljervisheath
210
24k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
61k
Making the Leap to Tech Lead
cromwellryan
135
9.7k
Site-Speed That Sticks
csswizardry
13
990
Optimizing for Happiness
mojombo
379
70k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Scaling GitHub
holman
464
140k
Typedesign – Prime Four
hannesfritz
42
2.9k
Faster Mobile Websites
deanohume
310
31k
Transcript
© John Chandler 2012 Regular Expressions
http://xkcd.com/208/
What? A pattern describing a set of text strings
When? From formal regular set theory in 1950s Popularised by
beardy UNIX types in 1970s
Why? Matching / searching text Search and replace Parsing strings
Main Flavours POSIX Basic / Extended (default with UNIX tools)
Perl-derived – very popular! (Perl, Python, PHP, JavaScript, .NET)
Syntax: The First 90% c standard characters . any character
^ start of string $ end of string c* zero or more of c
Syntax: The First 90% ^Pip*e.*$ matches: Piper at the Gates
of Dawn Pie and Chips
Syntax: The First 90% ^Pip*e.*$ doesn't match: A Pipe pie
spam chips and spam
Choices | or, alternative [abc] character class [^abc] inverse of
class
Choices foo|bar matches: foo fighters toolbar bar fool
Special Sequences \b beginning / end of word \d decimal
digit \s whitespace \w alphanumeric and underscore
Repetition s * zero or more + one or more
? zero or one {n} exactly n-times {n,m} n to m times
Groups (abc) capturing group (?:abc) non-capturing group (?P<name>) named capturing
group (?P=name) match a previous match
What do these do? (foo|bar) ((?:abc){3,4}) (?P<foo>\w+) [“']([^”']+)[“']
What about these? (fish) \1 (?P<thing>fish) (?P=thing) Isaac (?=Asimov) Isaac
(?!Asimov)
Greedy Matching By default, regular expressions are greedy. They will
find the biggest match that satisfies an expression.
Non-greedy Repetitions *? zero or more +? one or more
?? zero or one
Writing Regexes Don't if there is a simpler way Build
regex from small chunks Use the Python and PHP REPLs Document!
Debugging Regexes Don't panic Break regex into smaller chunks Use
the Python and PHP REPLs Watch out for special characters!
Anything else? Any questions? Read the Python and PHP docs
Try the UNIX grep command