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
12
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
11
Snowboarding Saved My Software Career
metaljoe
0
7
Neurodiversity in Tech
metaljoe
0
52
Neurodiversity Lightning Talk
metaljoe
0
10
Tackling a Legacy Codebase with (Micro)Services
metaljoe
0
6
Testing Workshop - Part 1 (PyConUK 2011)
metaljoe
0
17
Testing Workshop - Part 2 (PyConUK 2011)
metaljoe
0
12
Python, DOT, and Visualising Legacy Code The Lazy Way
metaljoe
0
17
Test Driven Development (A Short Introduction)
metaljoe
0
10
Other Decks in Programming
See All in Programming
AIプログラマーDevinは PHPerの夢を見るか?
shinyasaita
1
220
The Modern View Layer Rails Deserves: A Vision For 2025 And Beyond @ RailsConf 2025, Philadelphia, PA
marcoroth
1
170
ruby.wasmで多人数リアルタイム通信ゲームを作ろう
lnit
3
490
PipeCDのプラグイン化で目指すところ
warashi
1
280
AI時代の『改訂新版 良いコード/悪いコードで学ぶ設計入門』 / ai-good-code-bad-code
minodriven
14
5k
MDN Web Docs に日本語翻訳でコントリビュートしたくなる
ohmori_yusuke
1
120
GitHub Copilot and GitHub Codespaces Hands-on
ymd65536
2
150
プロダクト志向なエンジニアがもう一歩先の価値を目指すために意識したこと
nealle
0
130
Node-RED を(HTTP で)つなげる MCP サーバーを作ってみた
highu
0
120
ソフトウェア品質を数字で捉える技術。事業成長を支えるシステム品質の マネジメント
takuya542
1
13k
“いい感じ“な定量評価を求めて - Four Keysとアウトカムの間の探求 -
nealle
1
10k
ペアプロ × 生成AI 現場での実践と課題について / generative-ai-in-pair-programming
codmoninc
1
18k
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.3k
Making the Leap to Tech Lead
cromwellryan
134
9.4k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.9k
Practical Orchestrator
shlominoach
189
11k
Building a Modern Day E-commerce SEO Strategy
aleyda
42
7.4k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
35
2.4k
Making Projects Easy
brettharned
116
6.3k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
45
7.5k
Building an army of robots
kneath
306
45k
Docker and Python
trallard
44
3.5k
The Power of CSS Pseudo Elements
geoffreycrofte
77
5.9k
For a Future-Friendly Web
brad_frost
179
9.8k
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