Showing posts with label regex. Show all posts
Showing posts with label regex. Show all posts

Friday, August 14, 2015

ruby gsub doesnt escape single quotes

# ' means $' which is everything after the match. Escape the \ again and it works
# http://stackoverflow.com/questions/2180322/ruby-gsub-doesnt-escape-single-quotes
to_fix = "Sporty's"
result = to_fix.gsub(/'/, "\\\\'")
puts "result=#{result}"

Friday, July 31, 2015

ruby regex group match and assign variables on 1 line using captures


http://stackoverflow.com/questions/9303984/ruby-regexp-group-matching-assign-variables-on-1-line
#!/usr/bin/env ruby

string = "RyanOnRails: This is a test"
one, two, three = string.match(/(^.*)(:)(.*)/i).captures

p one   #=> "RyanOnRails"
p two   #=> ":"
p three #=> " This is a test"