Regular Expressions
A way of describing patterns within search strings
colt@gmail.com
carly.simon@yahoo.com
(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)
rosa-98@meals.org
david+lee+roth@hotmail.com
shoe_queen91@hello.net
Let's use a regular expression!
Starts with 1 or more letter,number,+,_,-,. then
A single @ sign then
1 or more letter, number, or - then
A single dot then
ends with 1 or more letter, number,-, or .
def hello_world(thingy):
#I do nothing
print("Hello World!")
I'm just going to cover some important ones
\d | digit 0-9 |
\w | letter, digit, or underscore |
\s | whitespace character |
\D | not a digit |
\W | not a word character |
\S | not a whitespace character |
. | any character except line break |
Some Characters
+ | One or more |
{3} | Exactly x times. {3} - 3 times |
{3,5} | Three to five times |
{4,} | Four or more times |
* | Zero or more times |
? | Once or none (optional) |
Quantifers
^ | Start of string or line |
$ | End of string or line |
\b | Word boundary |
Anchors and Boundaries
| (pipe character)
"Mr|Mrs|Ms"
#import regex module
import re
#define our phone number regex
pattern = re.compile(r'\d{3} \d{3}-\d{4}')
#search a string with our regex
res = pattern.search('Call me at 415 555-4242!')