Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Regexp-based Refactoring

Regexp-based Refactoring

Avatar for Remigijus Jodelis

Remigijus Jodelis

October 08, 2015

Other Decks in Programming

Transcript

  1. /^1?$|^(11+?)\1+$/ "", "1", "1111", "111111", "11111111", "111111111", … all the

    strings of 1 except those whose length is PRIME NUMBER ANSWER
  2. 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
  3. well... GIT, obviously ! > cd project > git init

    > git commit -am "status quo"
  4. 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
  5. 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
  6. 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>
  7. 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>
  8. 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
  9. points to consider match and count patterns -- before actually

    updating decide for each step -- edit manually or replace pattern?