Created
January 21, 2019 10:50
-
-
Save rodloboz/cd93910099bdcfc3bbc93e024a99378a to your computer and use it in GitHub Desktop.
Lecture on Regular Expression - Batch 224
This file contains hidden or 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
| 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}" |
This file contains hidden or 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
| 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