René's URL Explorer Experiment


Title: The Python re Module (Video) – Real Python

Open Graph Title: The Python re Module – Real Python

Description: In the previous lesson, I finished up the language of regular expressions and introduced the concept of grouping. In this lesson, I’ll be showing you how to use regular expressions inside of Python. First, a little review. The regex on the left has…

Open Graph Description: In the previous lesson, I finished up the language of regular expressions and introduced the concept of grouping. In this lesson, I’ll be showing you how to use regular expressions inside of Python. First, a little review. The regex on the left has…

Opengraph URL: https://realpython.com/lessons/python-re-module/

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
  {
    "@context": "https://schema.org",
    "@type": "VideoObject",
    "name": "The Python re Module",
    "description": "In the previous lesson, I finished up the language of regular expressions and introduced the concept of grouping. In this lesson, I’ll be showing you how to use regular expressions inside of Python. First, a little review. The regex on the left has…",
    "thumbnailUrl": ["https://files.realpython.com/media/Regular-Expressions-Regexes-in-Python-Part-1_Watermarked.0423050c5371.jpg"],
    "uploadDate": "2020-11-17T14:00:00+00:00",
    "duration": "PT8M29S",
    
    "potentialAction": {
      "@type": "SeekToAction",
      "target": "https://realpython.com/lessons/python-re-module/#t={seek_to_second_number}",
      "startOffset-input": "required name=seek_to_second_number"
    }
  }
  

authorReal Python
twitter:cardsummary_large_image
twitter:imagehttps://files.realpython.com/media/Regular-Expressions-Regexes-in-Python-Part-1_Watermarked.0423050c5371.jpg
og:imagehttps://files.realpython.com/media/Regular-Expressions-Regexes-in-Python-Part-1_Watermarked.0423050c5371.jpg
twitter:creator@realpython
og:typevideo.episode

Links:

https://realpython.com/
Start Herehttps://realpython.com/start-here/
Learn Python https://realpython.com/lessons/python-re-module/
Python Tutorials →In-depth articles and video courseshttps://realpython.com/search?kind=article&kind=course&order=newest
Learning Paths →Guided study plans for accelerated learninghttps://realpython.com/learning-paths/
Quizzes & Exercises →Check your learning progresshttps://realpython.com/quizzes/
Browse Topics →Focus on a specific area or skill levelhttps://realpython.com/tutorials/all/
Community Chat →Learn with other Pythonistashttps://realpython.com/community/
Office Hours →Live Q&A calls with Python expertshttps://realpython.com/office-hours/
Podcast →Hear what’s new in the world of Pythonhttps://realpython.com/podcasts/rpp/
Books →Round out your knowledge and learn offlinehttps://realpython.com/products/books/
Reference →Concise definitions for common Python termshttps://realpython.com/ref/
Code Mentor →BetaPersonalized code assistance & learning toolshttps://realpython.com/mentor/
Unlock All Content →https://realpython.com/account/join/
More https://realpython.com/lessons/python-re-module/
Learner Storieshttps://realpython.com/learner-stories/
Python Newsletterhttps://realpython.com/newsletter/
Python Job Boardhttps://www.pythonjobshq.com
Meet the Teamhttps://realpython.com/team/
Become a Tutorial Writerhttps://realpython.com/write-for-us/
Become a Video Instructorhttps://realpython.com/become-an-instructor/
Searchhttps://realpython.com/search
https://realpython.com/search
Joinhttps://realpython.com/account/join/
Sign‑Inhttps://realpython.com/account/login/?next=%2Flessons%2Fpython-re-module%2F
Unlock This Lessonhttps://realpython.com/account/join/?utm_source=rp_lesson&utm_content=building-regexes-python
Unlock This Lessonhttps://realpython.com/account/join/?utm_source=rp_lesson&utm_content=building-regexes-python
https://realpython.com/courses/building-regexes-python/#team
Regular Expressions and Building Regexes in Pythonhttps://realpython.com/courses/building-regexes-python/
Christopher Trudeauhttps://realpython.com/courses/building-regexes-python/#team
Recommended Tutorialhttps://realpython.com/regex-python/
Course Slides (.pdf)https://realpython.com/courses/building-regexes-python/downloads/regex-slides/
Sample Code (.zip)https://realpython.com/courses/building-regexes-python/downloads/regex-sample-code/
Ask a Questionhttps://realpython.com/lessons/python-re-module/#discussion
https://realpython.com/feedback/survey/course/building-regexes-python/liked/?from=lesson-title
https://realpython.com/feedback/survey/course/building-regexes-python/disliked/?from=lesson-title
Transcripthttps://realpython.com/lessons/python-re-module/#transcript
Discussion (8)https://realpython.com/lessons/python-re-module/#discussion
00:00https://realpython.com/lessons/python-re-module/#t=0.51
In the previous lesson,https://realpython.com/lessons/python-re-module/#t=0.51
I finished up the language of regular expressions and introduced the concept ofhttps://realpython.com/lessons/python-re-module/#t=1.8
grouping. In this lesson,https://realpython.com/lessons/python-re-module/#t=6.03
I’ll be showing you how to use regular expressions inside of Python.https://realpython.com/lessons/python-re-module/#t=8.039
00:13https://realpython.com/lessons/python-re-module/#t=13.38
First, a little review. The regex on the left has some grouping in it.https://realpython.com/lessons/python-re-module/#t=13.38
The square brackets indicate a character class of the vowels,https://realpython.com/lessons/python-re-module/#t=17.85
the parentheses around it are a group of that choice of vowel,https://realpython.com/lessons/python-re-module/#t=22.02
and the r is literal.https://realpython.com/lessons/python-re-module/#t=26.52
00:28https://realpython.com/lessons/python-re-module/#t=28.74
This highlights the 'ar', 'or', and 'er' in the string,https://realpython.com/lessons/python-re-module/#t=28.74
creating three groups matching an 'a', an 'o', and an 'e'. Laterhttps://realpython.com/lessons/python-re-module/#t=33.69
when I show you how to do this in Python,https://realpython.com/lessons/python-re-module/#t=38.16
you’ll actually be able to access this content. Here’s another one,https://realpython.com/lessons/python-re-module/#t=40.17
this time a character class of the digits 4 to 9,https://realpython.com/lessons/python-re-module/#t=45.06
and that is grouped,https://realpython.com/lessons/python-re-module/#t=48.66
and then one or more of those can happen using the plus (+) quantifier. Notice thathttps://realpython.com/lessons/python-re-module/#t=50.73
this matches the '45', the '9', and the '888',https://realpython.com/lessons/python-re-module/#t=56.61
but the grouping is the '5', the '9', and the '8'.https://realpython.com/lessons/python-re-module/#t=60.78
01:05https://realpython.com/lessons/python-re-module/#t=65.01
When you apply a quantifier to a group, only the last group gets counted.https://realpython.com/lessons/python-re-module/#t=65.01
So the '4' and the '5' matches the regex, but the '5' ends up in the group.https://realpython.com/lessons/python-re-module/#t=70.11
01:15https://realpython.com/lessons/python-re-module/#t=75.36
The '9' matches the regex and ends up in the group,https://realpython.com/lessons/python-re-module/#t=75.36
and the '888' matches the regex and the last '8' ends up in the group.https://realpython.com/lessons/python-re-module/#t=78.57
01:25https://realpython.com/lessons/python-re-module/#t=85.02
This regex looks for the literal lettershttps://realpython.com/lessons/python-re-module/#t=85.02
the grouped together, can have zero or more repetitions of any character, thenhttps://realpython.com/lessons/python-re-module/#t=87.09
uses a backreference.https://realpython.com/lessons/python-re-module/#t=94.74
01:36https://realpython.com/lessons/python-re-module/#t=96.51
So you’re looking for whatever matched the first group happening againhttps://realpython.com/lessons/python-re-module/#t=96.51
inside of the string.https://realpython.com/lessons/python-re-module/#t=100.56
So what you’re getting is a match between the words 'the'.https://realpython.com/lessons/python-re-module/#t=103.05
01:47https://realpython.com/lessons/python-re-module/#t=107.52
All of the red text matches, but only the 'the' ends up in the group.https://realpython.com/lessons/python-re-module/#t=107.52
01:53https://realpython.com/lessons/python-re-module/#t=113.79
You’ve been pretty patient with me up until now.https://realpython.com/lessons/python-re-module/#t=113.79
Everything’s been about regexes without really talking about Python,https://realpython.com/lessons/python-re-module/#t=116.34
so now I’m going to show you how to use this inside of Python.https://realpython.com/lessons/python-re-module/#t=120.03
02:04https://realpython.com/lessons/python-re-module/#t=124.14
The re—short for regular expression—library is a standard part of Python.https://realpython.com/lessons/python-re-module/#t=124.14
Most of the methods inside of the re module take a string pattern—https://realpython.com/lessons/python-re-module/#t=130.139
which is the regex—and a string to search against, and return a result.https://realpython.com/lessons/python-re-module/#t=134.55
02:19https://realpython.com/lessons/python-re-module/#t=139.38
The result is usually a Match object.https://realpython.com/lessons/python-re-module/#t=139.38
This Match object gives information about the match—https://realpython.com/lessons/python-re-module/#t=142.68
whether or not a match happened and what portions of the string matchedhttps://realpython.com/lessons/python-re-module/#t=146.13
the result. Match objects are truthy.https://realpython.com/lessons/python-re-module/#t=149.64
02:33https://realpython.com/lessons/python-re-module/#t=153.48
That means they can be compared as Booleans,https://realpython.com/lessons/python-re-module/#t=153.48
so you can use a re method that returns one of these objectshttps://realpython.com/lessons/python-re-module/#t=155.94
and then compare the object in an if statement to see whether or not a matchhttps://realpython.com/lessons/python-re-module/#t=160.71
happened. We’ll start out by importing the module.https://realpython.com/lessons/python-re-module/#t=164.76
02:52https://realpython.com/lessons/python-re-module/#t=172.23
This question will be my test string.https://realpython.com/lessons/python-re-module/#t=172.23
02:57https://realpython.com/lessons/python-re-module/#t=177.0
Using the search() method inside of the re module returnshttps://realpython.com/lessons/python-re-module/#t=177.0
a Match object. In this line, I’ve searched for the literal expressionhttps://realpython.com/lessons/python-re-module/#t=180.76
"spam" inside of the question. As a Match object was returned, that tells youhttps://realpython.com/lessons/python-re-module/#t=185.8
that 'spam' was successfully found within the question.https://realpython.com/lessons/python-re-module/#t=191.92
03:16https://realpython.com/lessons/python-re-module/#t=196.24
The span parameter inside of the Match object tells you where the matchhttps://realpython.com/lessons/python-re-module/#t=196.24
happened. This is between letters 7 and 11.https://realpython.com/lessons/python-re-module/#t=200.68
03:25https://realpython.com/lessons/python-re-module/#t=205.6
The numbers in the span are equivalent to a slice in a list or a slice of ahttps://realpython.com/lessons/python-re-module/#t=205.6
string,https://realpython.com/lessons/python-re-module/#t=210.37
so this indicates that it starts at 7 and finishes at the 10—https://realpython.com/lessons/python-re-module/#t=211.45
11 is the upper limit, not included.https://realpython.com/lessons/python-re-module/#t=216.61
03:39https://realpython.com/lessons/python-re-module/#t=219.94
This is the opposite of the curly brackets inside of the regular expressionshttps://realpython.com/lessons/python-re-module/#t=219.94
themselves.https://realpython.com/lessons/python-re-module/#t=223.66
It can be a little confusing as you switch back and forth between the twohttps://realpython.com/lessons/python-re-module/#t=225.28
mechanisms,https://realpython.com/lessons/python-re-module/#t=228.34
but the span parameter of the Match is closer to the Pythonic mechanism.https://realpython.com/lessons/python-re-module/#t=229.51
03:57https://realpython.com/lessons/python-re-module/#t=237.3
Here, you can see I’ve sliced question using the 7 and 11 from the span,https://realpython.com/lessons/python-re-module/#t=237.3
and I get back 'spam', the match from the string.https://realpython.com/lessons/python-re-module/#t=241.98
04:06https://realpython.com/lessons/python-re-module/#t=246.87
I’ll do that again, this time storing it in a variable.https://realpython.com/lessons/python-re-module/#t=246.87
04:13https://realpython.com/lessons/python-re-module/#t=253.11
Evaluating this variable as a Boolean returns True,https://realpython.com/lessons/python-re-module/#t=253.11
indicating that a match was found.https://realpython.com/lessons/python-re-module/#t=256.32
I can run a function called .span() on the Match object that returns the lower andhttps://realpython.com/lessons/python-re-module/#t=259.68
upper boundaries,https://realpython.com/lessons/python-re-module/#t=264.84
Another function called .start(), showing the lower boundary,https://realpython.com/lessons/python-re-module/#t=267.12
and finally .end() to give you the upper boundary.https://realpython.com/lessons/python-re-module/#t=271.74
04:36https://realpython.com/lessons/python-re-module/#t=276.81
The .string attribute shows you what was being matched against. Somewhathttps://realpython.com/lessons/python-re-module/#t=276.81
confusingly, the re module also has a function called match().https://realpython.com/lessons/python-re-module/#t=281.28
04:46https://realpython.com/lessons/python-re-module/#t=286.65
To be clear as I’m moving forward, if I’m talking about the function,https://realpython.com/lessons/python-re-module/#t=286.65
I will be explicit and say the match() function.https://realpython.com/lessons/python-re-module/#t=290.13
Otherwise, I’m talking about a resulting Match object.https://realpython.com/lessons/python-re-module/#t=293.55
04:58https://realpython.com/lessons/python-re-module/#t=298.41
The match() function matches the beginning of the string.https://realpython.com/lessons/python-re-module/#t=298.41
This is the equivalent of using a caret anchor (^) inside of your regex.https://realpython.com/lessons/python-re-module/#t=302.58
05:07https://realpython.com/lessons/python-re-module/#t=307.47
This did not return anything, and that’s because no match was found.https://realpython.com/lessons/python-re-module/#t=307.47
Let me do that again, this time storing it in a variable.https://realpython.com/lessons/python-re-module/#t=312.45
05:18https://realpython.com/lessons/python-re-module/#t=318.48
The variable doesn’t contain anything.https://realpython.com/lessons/python-re-module/#t=318.48
05:22https://realpython.com/lessons/python-re-module/#t=322.83
Comparing it to None shows that it’s True.https://realpython.com/lessons/python-re-module/#t=322.83
Or, converting it to a Boolean means it’s False.https://realpython.com/lessons/python-re-module/#t=326.79
This shows you how you could test the results of your regular expressionhttps://realpython.com/lessons/python-re-module/#t=330.84
functions inside of an if statement.https://realpython.com/lessons/python-re-module/#t=334.14
05:42https://realpython.com/lessons/python-re-module/#t=342.54
This regex was successful.https://realpython.com/lessons/python-re-module/#t=342.54
It’s looking for the repetition of 5 word-likehttps://realpython.com/lessons/python-re-module/#t=344.46
meta-characters. As the string starts with 'Lovel',https://realpython.com/lessons/python-re-module/#t=347.82
which are all word characters,https://realpython.com/lessons/python-re-module/#t=353.07
the match results showing the span of 0 to 5. Python 3.4https://realpython.com/lessons/python-re-module/#t=355.35
added a function called fullmatch().https://realpython.com/lessons/python-re-module/#t=360.53
06:06https://realpython.com/lessons/python-re-module/#t=366.88
As you might guess from its name,https://realpython.com/lessons/python-re-module/#t=366.88
it’s looking for a regular expression that matches the entire string.https://realpython.com/lessons/python-re-module/#t=368.17
Of course, looking for "spam", that’s not going to match the whole string,https://realpython.com/lessons/python-re-module/#t=374.29
06:20https://realpython.com/lessons/python-re-module/#t=380.86
so once again, you’re getting back a None object.https://realpython.com/lessons/python-re-module/#t=380.86
06:29https://realpython.com/lessons/python-re-module/#t=389.71
Let’s break this regular expression down. Looking at the inner group first,https://realpython.com/lessons/python-re-module/#t=389.71
there’s the word meta-character with zero or more instances,https://realpython.com/lessons/python-re-module/#t=394.84
there’s the whitespace character with zero or more instances.https://realpython.com/lessons/python-re-module/#t=398.53
06:41https://realpython.com/lessons/python-re-module/#t=401.98
So I’m looking for something that looks like an actual word. That is inside of ahttps://realpython.com/lessons/python-re-module/#t=401.98
group. That group repeats itself zero or more times,https://realpython.com/lessons/python-re-module/#t=407.2
and then is followed by an exclamation mark. All of that is grouped.https://realpython.com/lessons/python-re-module/#t=411.46
06:57https://realpython.com/lessons/python-re-module/#t=417.04
The outside group can be repeated zero or more times.https://realpython.com/lessons/python-re-module/#t=417.04
This is successful because it matches the two sub-parts of this string.https://realpython.com/lessons/python-re-module/#t=421.57
"Lovely spam!" and "Wonderful spam!" each match thehttps://realpython.com/lessons/python-re-module/#t=426.1
outer group. And because the outer group is repeated,https://realpython.com/lessons/python-re-module/#t=431.05
this regular expression matches the entire string,https://realpython.com/lessons/python-re-module/#t=434.5
giving a truth value for fullmatch().https://realpython.com/lessons/python-re-module/#t=437.71
07:22https://realpython.com/lessons/python-re-module/#t=442.18
Another function the library has is findall().https://realpython.com/lessons/python-re-module/#t=442.18
07:27https://realpython.com/lessons/python-re-module/#t=447.91
Unlike the other functions I’ve shown you so far,https://realpython.com/lessons/python-re-module/#t=447.91
findall() doesn’t return a Match object—it returns a list.https://realpython.com/lessons/python-re-module/#t=450.46
It applies the regular expression and finds each match inside of the string,https://realpython.com/lessons/python-re-module/#t=454.45
returning the matching characters in a list.https://realpython.com/lessons/python-re-module/#t=459.82
07:43https://realpython.com/lessons/python-re-module/#t=463.45
This regular expression is looking for a vowel, followed by not a vowel. Inside ofhttps://realpython.com/lessons/python-re-module/#t=463.45
"Lovely spam! Wonderful spam!"https://realpython.com/lessons/python-re-module/#t=468.55
you have 'ov' from 'Lovely', 'el' from 'Lovely',https://realpython.com/lessons/python-re-module/#t=470.29
'am' from 'spam', et cetera.https://realpython.com/lessons/python-re-module/#t=474.76
07:58https://realpython.com/lessons/python-re-module/#t=478.63
findall() returns a list. Sometimes instead of wanting a list, you want an iterator.https://realpython.com/lessons/python-re-module/#t=478.63
Enterhttps://realpython.com/lessons/python-re-module/#t=484.72
finditer(). It essentially does the same thing as findall() but returns an iteratorhttps://realpython.com/lessons/python-re-module/#t=485.35
instead of a list. This is more efficient in memoryhttps://realpython.com/lessons/python-re-module/#t=490.03
if you’re doing a large number of matches.https://realpython.com/lessons/python-re-module/#t=493.81
08:17https://realpython.com/lessons/python-re-module/#t=497.89
Well, that was your first exposure to using regular expressions inside of Python.https://realpython.com/lessons/python-re-module/#t=497.89
Next up, I’ll show you how to take advantage of grouped results.https://realpython.com/lessons/python-re-module/#t=503.32
Jan. 19, 2021https://realpython.com/lessons/python-re-module/#comment-5aa2c5c8-0dee-4df4-b168-f287d4ba3350
Jan. 19, 2021https://realpython.com/lessons/python-re-module/#comment-251debd9-d8b4-4fc6-ba8b-8e8410d194f9
Jan. 20, 2021https://realpython.com/lessons/python-re-module/#comment-db1172c2-0751-48ae-9bfc-ec82f656924f
Jan. 20, 2021https://realpython.com/lessons/python-re-module/#comment-16044be9-799b-4c96-9132-19596032280b
April 17, 2021https://realpython.com/lessons/python-re-module/#comment-d37a2db2-7416-4cff-acab-ea2e0fdd7fc2
April 17, 2021https://realpython.com/lessons/python-re-module/#comment-1811399d-d66e-45e3-b844-4d47c44c3cad
Dec. 12, 2023https://realpython.com/lessons/python-re-module/#comment-d2a89955-accc-441d-85f6-ed14e1b03b6b
Jan. 2, 2024https://realpython.com/lessons/python-re-module/#comment-9f785f2d-59e8-47f9-a8ac-08db3f582c6f
lookaheadhttps://realpython.com/regex-python/#lookahead-and-lookbehind-assertions
Become a Memberhttps://realpython.com/account/join/
https://realpython.com/lessons/regex-grouping/
Overviewhttps://realpython.com/courses/building-regexes-python/
https://realpython.com/lessons/accessing-groups/
Regular Expressions and Building Regexes in Python (Overview) 03:56 https://realpython.com/videos/building-regexes-overview/
Plain Matching and Class Matching 11:15 https://realpython.com/videos/plain-class-matching/
Meta-Characters 07:49 https://realpython.com/videos/meta-characters/
Regex Anchors 06:35 https://realpython.com/lessons/regex-anchors/
Regex Quantifiers 08:50 https://realpython.com/lessons/regex-quantifiers/
Regex Grouping 08:01 https://realpython.com/lessons/regex-grouping/
The Python re Module 08:29 https://realpython.com/lessons/python-re-module/
Accessing Groups 03:42 https://realpython.com/lessons/accessing-groups/
Naming Groups 11:56 https://realpython.com/lessons/naming-groups/
Substituting, Splitting, and Escaping 10:21 https://realpython.com/lessons/substitute-split-escape/
Creating Flags 11:34 https://realpython.com/lessons/creating-flags/
Advanced Matching 07:17 https://realpython.com/lessons/advanced-matching/
Fun and Further Reading 07:02 https://realpython.com/lessons/fun-further-reading/
Regular Expressions and Building Regexes in Python (Summary) 06:17 https://realpython.com/lessons/building-regexes-summary/
Privacy Policyhttps://realpython.com/privacy-policy/

Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover

Robots: max-image-preview:large


URLs of crawlers that visited me.