79 lines
2.6 KiB
Markdown
79 lines
2.6 KiB
Markdown
Regular Expressions
|
|
===================
|
|
|
|
Sometimes you gotta regulate on your friends and family. As programmers, we gotta regulate on strings.
|
|
|
|
Regular expressions are patterns to match your strings. At first they look like somebody just jammed the keyboard with their forehead, but don't fret - they will soon make sense.
|
|
|
|
Use [rubular](http://rubular.com/) to test your regular expressions.
|
|
```
|
|
[A-Z]
|
|
```
|
|
This is a range of capital letters. Any one capital letter matches this pattern.
|
|
```
|
|
[A-Z]{3,}
|
|
```
|
|
This is same range of capital letters, with a condition that it is 3 characters or more.
|
|
```
|
|
[A-Za-z0-9]{6}
|
|
```
|
|
The first range lets us use any letter, capital or lowercase, and any number, in any order. There must be 6 characters to match.
|
|
```
|
|
^LOL\s[A-Za-z]*
|
|
```
|
|
Ok, now it looks weird. The ^ means that the string must start with the first expression, which is LOL. \s means whitespace. And it ends with a range of all letters. * means that that range of all letters can be any length. Here's what matches:
|
|
```py
|
|
'LOL Hi' # Matches
|
|
'LOL whatsupdog' # Matches
|
|
'LOLyo' # doesn't match, no whitespace
|
|
'LOL hey d00d' # doesnt match, only letters allowed
|
|
'whats up' # doesn't match, must start with LOL
|
|
```
|
|
You got it? Sandbox in rubular and read the quick reference guide. Write your answers in the answers.md file.
|
|
|
|
#### Round 1: Name
|
|
|
|
Make a regex pattern that matches a properly written name, like the following:
|
|
|
|
Miley Cyrus
|
|
Justin Bieber
|
|
Katy Perry
|
|
|
|
Notice the pattern - the first letter of the first name is capitalized, the rest is lower case. There is a space. The first letter of the last name is capitalized, the rest is lower case.
|
|
|
|
#### Round 2: Phone Number
|
|
|
|
Write a regex pattern that validates a correct phone number, like any of the following:
|
|
|
|
212-555-1023
|
|
(917)888-2424
|
|
5164329123
|
|
|
|
Area codes cannot start with 0 or 1. There may be parentheses around the area code. There must be a set of 3 numbers (area code) a set of 3 numbers(city) and set of 4 numbers. There may be dashes between them. There must be 10 numbers in total, no more no less.
|
|
|
|
#### Round 3: Email Address
|
|
|
|
Write a regex pattern that validates a correct email address, like the following:
|
|
|
|
sales@somestore.com
|
|
roger.smith@yahoo.net
|
|
hi@my-domain.info
|
|
|
|
#### Round 4: URL
|
|
|
|
Write a regex pattern to validate URLs, like the following:
|
|
|
|
http://www.google.com
|
|
http://yahoo.net
|
|
http://my-site.github.io
|
|
|
|
#### Round 5: grep with regex
|
|
|
|
Now use regex with grep to search files on your computer!
|
|
|
|
Search for all files that end in .py
|
|
Search for all files that end in .jpg
|
|
Search for all files that contain your name
|
|
|
|
Have fun and sandbox.
|