Skip to content

Instantly share code, notes, and snippets.

@rodloboz
Created January 21, 2019 10:50
Show Gist options
  • Select an option

  • Save rodloboz/cd93910099bdcfc3bbc93e024a99378a to your computer and use it in GitHub Desktop.

Select an option

Save rodloboz/cd93910099bdcfc3bbc93e024a99378a to your computer and use it in GitHub Desktop.
Lecture on Regular Expression - Batch 224
PATTERN1 = /^(?<first_name>\w+)\.(?<last_name>\w+)@(?<provider>.+)\.(.+)$/
PATTERN2 = /^(\w+)\.(\w+)@(.+)\.(.+)$/
email = "[email protected]"
match = email.match(PATTERN1) # same as PATTERN.match(email)
p match
provider = match[:provider]
first_name = match[:first_name]
puts "#{first_name.capitalize}'s email provider is #{provider}"
PATTERN = /^\+\d{2}\s\d{2}\s\d{4}\s\d{4}$/
phone_number = " 20 7946 0234"
if phone_number =~ PATTERN
puts "This is a valid UK international phone number"
else
puts "It's not valid!"
end
text = "
this is a multi-line text
try to match only this line
and not the others
try
"
#.start of line. end of line
p text =~ /^try .* line$/
#start of string. end of string
p text =~ /\Atry .* line\z/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment