Learn RE – Regular Expressions in Python

You are here: Home » MiltonMarketing Blog » Computer Programming Languages » Python » Learn RE – Regular Expressions in Python

Learn RE – Regular Expressions in Python

Learn RE – Regular Expressions in Python: Regular Expressions (sometimes shortened to regexp, regex, or re) are a tool for matching patterns in text.

In Python, we have the re module. The applications for regular expressions are wide-spread, but they are fairly complex, so when contemplating using a regex for a certain task, think about alternatives, and come to regexes as a last resort.

An example regex is:

r"^(From|To|Cc).*[email protected]"

Now for an explanation: the caret ^ matches text at the beginning of a line. The following group, the part with (From|To|Cc) means that the line has to start with one of the words that are separated by the pipe |. That is called the OR operator, and the regex will match if the line starts with any of the words in the group. The .*? means to un-greedily match any number of characters, except the newline \n character. The un-greedy part means to match as few repetitions as possible. The . character means any non-newline character, the * means to repeat 0 or more times, and the ? character makes it un-greedy.

So, the following lines would be matched by that regex: From: [email protected] To: !asp]<,. [email protected]

A complete reference for the re syntax is available at the python docs (re-regular expressions.)

As an example of a “proper” email-matching regex (like the one in this exercise), see this: Mail::RFC822::Address: regexp-based address validation

# Exercise: make a regular expression that will match an email. this is the answer code
import re
def test_email(your_pattern):
    pattern = re.compile(your_pattern)
    emails = ["[email protected]", "[email protected]", "wha.t.`1an?ug{}[email protected]"]
    for email in emails:
        if not re.match(pattern, email):
            print("You failed to match %s" % (email))
        elif not your_pattern:
            print("Forgot to enter a pattern!")
# Your pattern here!
pattern = r"\"?([-a-zA-Z0-9.`?{}][email protected]\w+\.\w+)\"?"

Trinket.io on-line Python compiler

By |2019-02-08T19:19:41-04:00April 15th, 2018|Categories: Computer Programming Languages, Python|Tags: , , |

About the Author:

I am a loving father, & husband. I am a computer enthusiast. I have used and enjoyed computers since I was young and I enjoy teaching young minds how to code, because it teaches them how to think. Today with YouTube, and social media garbage our youth are losing the ability to think on their own and solve problems. I believe this is a serious epidemic as kids today dont understand that technology is a tool. This tool is being abused, and its underlying effects are taking its toll on kids behaviour, and learning.