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
Regular Expressions
Search
John Chandler
November 22, 2012
Programming
0
13
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
9
Neurodiversity in Tech
metaljoe
0
53
Neurodiversity Lightning Talk
metaljoe
0
12
Tackling a Legacy Codebase with (Micro)Services
metaljoe
0
8
Testing Workshop - Part 1 (PyConUK 2011)
metaljoe
0
18
Testing Workshop - Part 2 (PyConUK 2011)
metaljoe
0
13
Python, DOT, and Visualising Legacy Code The Lazy Way
metaljoe
0
18
Test Driven Development (A Short Introduction)
metaljoe
0
11
Other Decks in Programming
See All in Programming
Improving my own Ruby thereafter
sisshiki1969
1
160
Putting The Genie in the Bottle - A Crash Course on running LLMs on Android
iurysza
0
140
FindyにおけるTakumi活用と脆弱性管理のこれから
rvirus0817
0
500
AIコーディングAgentとの向き合い方
eycjur
0
270
基礎から学ぶ大画面対応(Learning Large-Screen Support from the Ground Up)
tomoya0x00
0
430
Updates on MLS on Ruby (and maybe more)
sylph01
1
180
デザイナーが Androidエンジニアに 挑戦してみた
874wokiite
0
350
Namespace and Its Future
tagomoris
6
700
Introducing ReActionView: A new ActionView-compatible ERB Engine @ Rails World 2025, Amsterdam
marcoroth
0
680
AI Coding Agentのセキュリティリスク:PRの自己承認とメルカリの対策
s3h
0
200
請來的 AI Agent 同事們在寫程式時,怎麼用 pytest 去除各種幻想與盲點
keitheis
0
120
OSS開発者という働き方
andpad
5
1.7k
Featured
See All Featured
Optimizing for Happiness
mojombo
379
70k
Navigating Team Friction
lara
189
15k
Speed Design
sergeychernyshev
32
1.1k
Rails Girls Zürich Keynote
gr2m
95
14k
Art, The Web, and Tiny UX
lynnandtonic
303
21k
GraphQLとの向き合い方2022年版
quramy
49
14k
Balancing Empowerment & Direction
lara
3
620
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
36
2.5k
Raft: Consensus for Rubyists
vanstee
140
7.1k
jQuery: Nuts, Bolts and Bling
dougneiner
64
7.9k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
Unsuck your backbone
ammeep
671
58k
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