Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Regexp-based Refactoring
Search
Remigijus Jodelis
October 08, 2015
Programming
36
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Regexp-based Refactoring
Remigijus Jodelis
October 08, 2015
Other Decks in Programming
See All in Programming
Long Living Tokens, Die, Die, Die! (or: The One Ring is Banned in Modern)
sylph01
0
300
JIS keyboard support in Omarchy
lef237
0
110
Railsのためのパスキー実践入門 〜マルチテナントSaaSでの意思決定〜
utakaha
2
560
動作中のプログラムの中身をリアルタイムに覗く / Realtime Debugger for CSharp with Roslyn
prota
1
1.9k
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
7
13k
半永久的に提供し続けられるプライベートクラウドを目指して ― 利用者の認知負荷を抑えるAPI抽象化とハードウェア世代交代の基盤設計
tomokon
0
500
ActionController meets Protocol Buffers
sorah
0
520
UnityでSystem.Net.WebSocketsなWebSocketサーバが動かないのでUnity Monoのコードを覗いてみた / about implementing websocket server with unity mono
drumath2237
1
610
Simple Storage Service(S3) is not simple
iwatsukayura
0
290
R8 の設定見直しでアプリサイズ削減
andpad
0
220
Verilogで学ぶCPU自作入門
uyuki234
8
4.1k
AIで開発が速くなった先で、開発チームは何を目指すか
shihochan
0
1.2k
Featured
See All Featured
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.7k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
4
1.2k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Rails Girls Zürich Keynote
gr2m
96
14k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
460
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
300
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
The agentic SEO stack - context over prompts
schlessera
0
970
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
What's in a price? How to price your products and services
michaelherold
247
13k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
600
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
2
2.2k
Transcript
Regexp-based Refactoring VilniusRB meetup 2015-10-08
Remigijus Jodelis @remigijusj
Part 0. Intro to regexps
regular expressions special language for search patterns
regexp examples /.*\.txt$/ => "hello.txt", ".txt" /#[0-9A-F]{6}/ => "color: #AA88E7;"
/\d{4}-\d{2}-\d{2}/ => "today is 2015-10-08"
regexp operations string[/answer: (yes|no|(maybe))/, 2] # nil, "maybe" string.gsub(/<.*?>/, "\n")
# "This:<br/><p></p>" => "This:\n\n\n"
regexp operations url = "http://[a-zA-Z0-9/.?=+]*" string.scan(/<a href="(#{url})"/) # [["http://..."], ...]
QUESTION what does it match? /^1?$|^(11+?)\1+$/
/^1?$|^(11+?)\1+$/ "", "1", "1111", "111111", "11111111", "111111111", … all the
strings of 1 except those whose length is PRIME NUMBER ANSWER
Part 1. Challenge
let me tell you a story
None
None
None
stats / sloc ------------------------------------------------------------------------------- Language files blank comment code -------------------------------------------------------------------------------
PHP (not really..) 14 7130 573 21648 CSS 1 625 40 4835 Javascript 1 1 0 12 ------------------------------------------------------------------------------- Total size: 1.3 MB
None
worst kind of duplication: lots of text with small anomalies
everywhere
problems difficult to find & fix layout errors hard to
make further modifications
challenge accepted
Part 2. Progress
None
well... GIT, obviously ! > cd project > git init
> git commit -am "status quo"
next: clean-up white-space and style
keeping strict style is important makes patterns simpler
need an idea how to start for real
remember this?
None
identifiers come in different contexts <div id="Ellipse2copy6"> <img src="images/kubas_2X2/images/Ellipse2copy7.png" $("#Layer10_ellipse2_mini").hide();
#Layer50_ellipse2_mini_m6 { position: absolute;
useful tool: extract.rb rx = Regexp.new(pattern) freq = Hash.new(0) File.read(path).scan(rx)
do |it| freq[it] += 1 end freq.keys.sort.each do |word| puts "%s -> %d" % [word, freq[word]] end
regexps: here they come /\bid="(.*?)"/ /#(\w+)\s*\{/ /\/(\w+)\.png/
basic operations • edit: manually • grep: search project files
(list of lines) • count: search, but only check the number • replace: pattern to replacement • interactive replace • test: manually and/or automatically
regexps: real life ^ {6}<label id="yes(\d+)" class="slot circle-(\d+)">\1\r\n \s {8}<input
type="radio" name="siena\1([abcd][123456])" id=" siena\1\3" value="s\1" />\r\n {6}</label>\r\n <label id="yes3" class="slot circle-2">3 <input type="radio" name="siena3b4" id="siena3b4" value="s3" /> </label>
how do i match this <label id="yes3"> <input type="radio" name="siena3b4"
id="siena3b4" value="s3" /> <div class="circle-off" id="Ellipse2_back"><img src=" images/circle_grey.png" width="131" height="57" /></div> <div class="circle-sel" id="Ellipse2_visited_back"><img src=" images/circle_green.png" width="131" height="57" /></div> <div class="circle-num" id="_3">3</div> </label>
most complicated regexp ^ {6}<label id="yes(\d+)">\r\n {8}<input type="radio" name=" siena\1([abcd][123456])"
id="siena\1\2" value="s\1" />\r\n {8} <div class="circle-off" id="(Ellipse\d+)(_back)?"><img src=" images/circle_grey.png" width="(\d+)" height="(\d+)" /></div>\r\n {8}<div class="circle-sel" id="\3_visited\4?"><img src="images/circle_green.png" width="\5" height="\6" /></div>\r\n {8}<div class="circle-num" id="_\1">\1</div>\r\n {6}</label>\r\n
None
points to consider match and count patterns -- before actually
updating decide for each step -- edit manually or replace pattern?
points to consider systematically increase order, remove anomalies maintain a
todo list
other usual refactoring steps • splitting modules • renaming functions
• moving code around • etc
how does code size decrease?
2 weeks and 80 commits later … 1.3 MB got
reduced to 270 kB
SUCCESS!
Part 3. Summing-up
there are ways to deal with copy-pasted code but it’s
not trivial
is it worth it?
lesson learned bulk-updating code is not (that) scary
lesson learned regexps are extremely useful, sometimes
so, next time?
None
just kidding ☺
Thank You!