Created
August 16, 2022 19:50
-
-
Save kernelsmith/d7b88c55e7057d314e7341c3858dfd50 to your computer and use it in GitHub Desktop.
Advanced (for me anyway) Regex stuff in the Ruby language that I often forget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruby_regex | |
# Capture groups | |
if /(hello) (world)/ =~ "hello world" | |
puts "#{$2} #{$1}" # => "world hello" | |
end | |
# Named groups | |
if /(?<domain>rubycademy)/ =~ 'https://www.rubycademy.com' | |
puts domain # => "rubycademy" | |
end | |
# Named groups w/the match method | |
if projects = /(?<domain>rubycademy)/.match('https://www.rubycademy.com') | |
puts projects['domain'] # => "rubycademy" | |
end | |
# Capture groups w/Back references | |
# the \1 refers to the first capture group which is (hello) | |
/(hello) \1/ =~ "hello hello" # => 0 | |
# you can access capture groups outside of the regular expression by using back-references | |
# capture everything between an html/xml tag | |
html[/<(html)>(.+)<\/\1>/m, 2] | |
# the \1 back-reference to the first capture group (html) | |
# accessing the capture group outisde of the regex | |
# a regexp that is the substring to replace, it includes (http) & excludes the s ((?!s)) | |
# the replacing string with a back-reference to the first capture group (http) | |
# (?!) is negative look ahead https://ruby-doc.org/core-2.7.2/Regexp.html#class-Regexp-label-Anchors | |
def to_https(url) | |
url.gsub(/(?<scheme>http)(?!s)/, '\k<scheme>s') | |
end | |
# in the 2nd arg we use \k<label> to back-ref the named group in the pattern passed as 1st arg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment