René's URL Explorer Experiment


Title: Get All Matches as Match Objects (Video) – Real Python

Open Graph Title: Get All Matches as Match Objects – Real Python

Description: In this lesson, you’ll take a look how you can find all occurrences of a substring using regular expressions, and then also preserve a lot of information about them by yielding Match objects. And to do this, you can use re.finditer() and then pass…

Open Graph Description: In this lesson, you’ll take a look how you can find all occurrences of a substring using regular expressions, and then also preserve a lot of information about them by yielding Match objects. And to do this, you can use re.finditer() and then pass…

Opengraph URL: https://realpython.com/lessons/get-match-objects-python/

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
  {
    "@context": "https://schema.org",
    "@type": "VideoObject",
    "name": "Get All Matches as Match Objects",
    "description": "In this lesson, you’ll take a look how you can find all occurrences of a substring using regular expressions, and then also preserve a lot of information about them by yielding Match objects. And to do this, you can use re.finditer() and then pass…",
    "thumbnailUrl": ["https://files.realpython.com/media/How-to-Check-if-a-String-Contains-Another-String_Watermarked.ad5138507ec7.jpg"],
    "uploadDate": "2023-04-04T14:00:00+00:00",
    "duration": "PT3M26S",
    
    "potentialAction": {
      "@type": "SeekToAction",
      "target": "https://realpython.com/lessons/get-match-objects-python/#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/How-to-Check-if-a-String-Contains-Another-String_Watermarked.ad5138507ec7.jpg
og:imagehttps://files.realpython.com/media/How-to-Check-if-a-String-Contains-Another-String_Watermarked.ad5138507ec7.jpg
twitter:creator@realpython
og:typevideo.episode

Links:

https://realpython.com/
Start Herehttps://realpython.com/start-here/
Learn Python https://realpython.com/lessons/get-match-objects-python/
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/get-match-objects-python/
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%2Fget-match-objects-python%2F
Unlock This Lessonhttps://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-string-contains-substring
Unlock This Lessonhttps://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-string-contains-substring
https://realpython.com/courses/python-string-contains-substring/#team
Check if a Python String Contains a Substringhttps://realpython.com/courses/python-string-contains-substring/
Martin Breusshttps://realpython.com/courses/python-string-contains-substring/#team
Recommended Tutorialhttps://realpython.com/python-string-contains-substring/
Course Slides (PDF)https://realpython.com/courses/python-string-contains-substring/downloads/python-string-contains-substring-slides/
Sample Code (ZIP)https://realpython.com/courses/python-string-contains-substring/downloads/python-string-contains-substring-code/
Ask a Questionhttps://realpython.com/lessons/get-match-objects-python/#discussion
https://realpython.com/feedback/survey/course/python-string-contains-substring/liked/?from=lesson-title
https://realpython.com/feedback/survey/course/python-string-contains-substring/disliked/?from=lesson-title
Transcripthttps://realpython.com/lessons/get-match-objects-python/#transcript
Discussionhttps://realpython.com/lessons/get-match-objects-python/#discussion
00:00https://realpython.com/lessons/get-match-objects-python/#t=0.63
In this lesson,https://realpython.com/lessons/get-match-objects-python/#t=0.63
you’ll take a look how you can find all occurrences of a substring usinghttps://realpython.com/lessons/get-match-objects-python/#t=1.34
regular expressions,https://realpython.com/lessons/get-match-objects-python/#t=5.3
and then also preserve a lot of information about them by yielding Matchhttps://realpython.com/lessons/get-match-objects-python/#t=6.62
objects. And to do this,https://realpython.com/lessons/get-match-objects-python/#t=11.3
you can use re.finditer() and then pass ithttps://realpython.com/lessons/get-match-objects-python/#t=13.75
again your pattern, and then also where you’re searching.https://realpython.com/lessons/get-match-objects-python/#t=18.43
00:21https://realpython.com/lessons/get-match-objects-python/#t=21.59
So the pattern that you used before was a match group aroundhttps://realpython.com/lessons/get-match-objects-python/#t=21.59
"secret",https://realpython.com/lessons/get-match-objects-python/#t=26.35
and then you wanted to look for the two occurrences of "secret" thathttps://realpython.com/lessons/get-match-objects-python/#t=28.45
are followed by either a dot or a comma. Okay,https://realpython.com/lessons/get-match-objects-python/#t=33.39
that’s your pattern,https://realpython.com/lessons/get-match-objects-python/#t=37.75
and then you want to search inside of text_lower.https://realpython.com/lessons/get-match-objects-python/#t=38.47
00:44https://realpython.com/lessons/get-match-objects-python/#t=44.72
So this actually collects all of them in an iterator.https://realpython.com/lessons/get-match-objects-python/#t=44.72
So if you run this, then you can see that you get back a callable iterator,https://realpython.com/lessons/get-match-objects-python/#t=48.24
which right now this doesn’t help a lot if you don’t loop over it or if youhttps://realpython.com/lessons/get-match-objects-python/#t=52.51
don’t save it anywhere. So let’s go ahead and do that.https://realpython.com/lessons/get-match-objects-python/#t=56.51
00:59https://realpython.com/lessons/get-match-objects-python/#t=59.77
I’m going to say for match in and then thishttps://realpython.com/lessons/get-match-objects-python/#t=59.77
iterator.https://realpython.com/lessons/get-match-objects-python/#t=64.35
01:07https://realpython.com/lessons/get-match-objects-python/#t=67.25
And then let’s just start by printing out match.https://realpython.com/lessons/get-match-objects-python/#t=67.25
01:13https://realpython.com/lessons/get-match-objects-python/#t=73.02
Okay. You can see, again,https://realpython.com/lessons/get-match-objects-python/#t=73.02
you’re getting these useful Match objects where you have a ton of information inhttps://realpython.com/lessons/get-match-objects-python/#t=74.27
there, like for example, where do the substrings start and where do they end?https://realpython.com/lessons/get-match-objects-python/#t=78.71
01:23https://realpython.com/lessons/get-match-objects-python/#t=83.37
And then also the actual match that you got. So that’s pretty neat.https://realpython.com/lessons/get-match-objects-python/#t=83.37
And again,https://realpython.com/lessons/get-match-objects-python/#t=88.28
you can use methods on these Match objects such as .group() orhttps://realpython.com/lessons/get-match-objects-python/#t=88.84
.span() that you used before and just work more with information of thosehttps://realpython.com/lessons/get-match-objects-python/#t=93.67
substrings that you identified. I’m going to give you an example.https://realpython.com/lessons/get-match-objects-python/#t=98.36
01:41https://realpython.com/lessons/get-match-objects-python/#t=101.48
Instead of just printing the match, let’s say group(1),https://realpython.com/lessons/get-match-objects-python/#t=101.48
and that just means you want to get the first match group,https://realpython.com/lessons/get-match-objects-python/#t=105.86
which in this case is "secret",https://realpython.com/lessons/get-match-objects-python/#t=108.72
and you don’t need to pass 1 in here.https://realpython.com/lessons/get-match-objects-python/#t=112.22
01:54https://realpython.com/lessons/get-match-objects-python/#t=114.6
You could just use .group() without an argument, like you did in a former lesson.https://realpython.com/lessons/get-match-objects-python/#t=114.6
If you don’t pass an argument,https://realpython.com/lessons/get-match-objects-python/#t=118.81
then you get the same as when using .group() and passing zero.https://realpython.com/lessons/get-match-objects-python/#t=119.96
02:03https://realpython.com/lessons/get-match-objects-python/#t=123.77
It gives you back the complete match string. In this case,https://realpython.com/lessons/get-match-objects-python/#t=123.77
that would be "secret." with the dot and "secret," with the comma.https://realpython.com/lessons/get-match-objects-python/#t=127.08
But if you’ve used capturing groups in a regular expression pattern,https://realpython.com/lessons/get-match-objects-python/#t=131.98
then you can also access these capturing groups by index,https://realpython.com/lessons/get-match-objects-python/#t=135.51
and here you’re fetching the first group.https://realpython.com/lessons/get-match-objects-python/#t=139.38
02:22https://realpython.com/lessons/get-match-objects-python/#t=142.92
And let’s also print match.span()https://realpython.com/lessons/get-match-objects-python/#t=142.92
to repeat the same methods that you tried out before.https://realpython.com/lessons/get-match-objects-python/#t=147.81
And you’ll see—or actually let’s put them in one line.https://realpython.com/lessons/get-match-objects-python/#t=152.8
02:44https://realpython.com/lessons/get-match-objects-python/#t=164.42
So you’ll have the information of the captured "secret" and alsohttps://realpython.com/lessons/get-match-objects-python/#t=164.42
its location, and like before, you could also create two match groups.https://realpython.com/lessons/get-match-objects-python/#t=169.21
02:56https://realpython.com/lessons/get-match-objects-python/#t=176.23
And then we can say match group 1, a secret, it’s at this location.https://realpython.com/lessons/get-match-objects-python/#t=176.23
To mix it up a bit,https://realpython.com/lessons/get-match-objects-python/#t=181.18
I’m going to do the match group 2 over here,https://realpython.com/lessons/get-match-objects-python/#t=182.16
and then you’ll see that it also prints out which one it was.https://realpython.com/lessons/get-match-objects-python/#t=187.04
03:10https://realpython.com/lessons/get-match-objects-python/#t=190.72
So this was the "secret" with the dot after,https://realpython.com/lessons/get-match-objects-python/#t=190.72
and the "secret" at that location is the one with the comma after.https://realpython.com/lessons/get-match-objects-python/#t=192.42
And of course, yeah,https://realpython.com/lessons/get-match-objects-python/#t=196.82
you can work much more with those Match objects because it actually preserves ahttps://realpython.com/lessons/get-match-objects-python/#t=197.96
lot of information about the substring that you mentioned in the string.https://realpython.com/lessons/get-match-objects-python/#t=201.68
Become a Memberhttps://realpython.com/account/join/
https://realpython.com/lessons/find-pattern-matches-python/
Overviewhttps://realpython.com/courses/python-string-contains-substring/
https://realpython.com/lessons/substrings-regex-python-summary/
Check if a Python String Contains a Substring (Overview) 02:00 https://realpython.com/videos/python-string-contains-substring-overview/
Confirm the Presence of a Substring 04:31 https://realpython.com/videos/confirm-substring-python/
Generalize Your Substring Check 04:10 https://realpython.com/videos/generalize-substring-check/
Learn More Using String Methods 01:49 https://realpython.com/lessons/string-methods-python-substring/
Use .count() to Get the Number of Occurrences 02:30 https://realpython.com/lessons/count-substring-python/
Use .index() to Get the Location of the Substring 02:35 https://realpython.com/lessons/index-python-substring/
Avoid Using .find() to Check for Substrings 05:20 https://realpython.com/lessons/avoid-find-python-substring/
Get to Know Regex for Finding Substrings 01:32 https://realpython.com/lessons/substrings-regex-python-intro/
Match a Substring Using re.search() 03:51 https://realpython.com/lessons/substring-re-search-python/
Explore a Match Object 03:25 https://realpython.com/lessons/match-object-substring/
Find All Pattern Matches 03:28 https://realpython.com/lessons/find-pattern-matches-python/
Get All Matches as Match Objects 03:26 https://realpython.com/lessons/get-match-objects-python/
Wrap Up Finding Substrings With Regex 01:51 https://realpython.com/lessons/substrings-regex-python-summary/
Set Up and Inspect the Data 02:10 https://realpython.com/lessons/inspect-pandas-data/
Filter DataFrame Rows With .str.contains() 02:03 https://realpython.com/lessons/filter-dataframe-str-contains/
Use Regular Expressions With pandas 02:23 https://realpython.com/lessons/regular-expressions-pandas/
Check if a Python String Contains a Substring (Quiz) 11:00 https://realpython.com/lessons/python-string-contains-substring-quiz/
Check if a Python String Contains a Substring (Summary) 02:22 https://realpython.com/lessons/python-string-contains-substring-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.