REGEX

Regular Expressions

What are regular Expressions?

A way of describing patterns within search strings

validating Emails

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 .  

Potential Use Cases

  • Credit Card number validating
  • Phone number validating
  • Advanced find/replace in text
  • Formatting text/output
  • Syntax highlighting
def hello_world(thingy):
    #I do nothing
    print("Hello World!")

First,

let's start with

REGEX outside of Python

There are a ton of regex symbols.

I'm just going to cover some important ones

SOME REGEX SYNTAX

\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

SOME REGEX SYNTAX

+ 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

SOME REGEX SYNTAX

^ Start of string or line
$ End of string or line
\b Word boundary

Anchors and Boundaries

LOGICAL OR

| (pipe character)

"Mr|Mrs|Ms"

I could make 20+ slides on Regex syntax. 

It's easier to look at a cheat sheet

NOW,

Let's Move on TO

USING REGEX WITH PYTHON

Using Regex in Python

#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!')