|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/get-match-objects-python/ |
| Python Tutorials →In-depth articles and video courses | https://realpython.com/search?kind=article&kind=course&order=newest |
| Learning Paths →Guided study plans for accelerated learning | https://realpython.com/learning-paths/ |
| Quizzes & Exercises →Check your learning progress | https://realpython.com/quizzes/ |
| Browse Topics →Focus on a specific area or skill level | https://realpython.com/tutorials/all/ |
| Community Chat →Learn with other Pythonistas | https://realpython.com/community/ |
| Office Hours →Live Q&A calls with Python experts | https://realpython.com/office-hours/ |
| Podcast →Hear what’s new in the world of Python | https://realpython.com/podcasts/rpp/ |
| Books →Round out your knowledge and learn offline | https://realpython.com/products/books/ |
| Reference →Concise definitions for common Python terms | https://realpython.com/ref/ |
| Code Mentor →BetaPersonalized code assistance & learning tools | https://realpython.com/mentor/ |
| Unlock All Content → | https://realpython.com/account/join/ |
|
More
| https://realpython.com/lessons/get-match-objects-python/ |
| Learner Stories | https://realpython.com/learner-stories/ |
| Python Newsletter | https://realpython.com/newsletter/ |
| Python Job Board | https://www.pythonjobshq.com |
| Meet the Team | https://realpython.com/team/ |
| Become a Tutorial Writer | https://realpython.com/write-for-us/ |
| Become a Video Instructor | https://realpython.com/become-an-instructor/ |
| Search | https://realpython.com/search |
| https://realpython.com/search |
| Join | https://realpython.com/account/join/ |
| Sign‑In | https://realpython.com/account/login/?next=%2Flessons%2Fget-match-objects-python%2F |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-string-contains-substring |
| Unlock This Lesson | https://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 Substring | https://realpython.com/courses/python-string-contains-substring/ |
| Martin Breuss | https://realpython.com/courses/python-string-contains-substring/#team |
| Recommended Tutorial | https://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 Question | https://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 |
| Transcript | https://realpython.com/lessons/get-match-objects-python/#transcript |
| Discussion | https://realpython.com/lessons/get-match-objects-python/#discussion |
| 00:00 | https://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 using | https://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 Match | https://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 it | https://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:21 | https://realpython.com/lessons/get-match-objects-python/#t=21.59 |
| So the pattern that you used before was a match group around | https://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" that | https://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:44 | https://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 you | https://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:59 | https://realpython.com/lessons/get-match-objects-python/#t=59.77 |
| I’m going to say for match in and then this | https://realpython.com/lessons/get-match-objects-python/#t=59.77 |
| iterator. | https://realpython.com/lessons/get-match-objects-python/#t=64.35 |
| 01:07 | https://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:13 | https://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 in | https://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:23 | https://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() or | https://realpython.com/lessons/get-match-objects-python/#t=88.84 |
| .span() that you used before and just work more with information of those | https://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:41 | https://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:54 | https://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:03 | https://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:22 | https://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:44 | https://realpython.com/lessons/get-match-objects-python/#t=164.42 |
| So you’ll have the information of the captured "secret" and also | https://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:56 | https://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:10 | https://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 a | https://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 Member | https://realpython.com/account/join/ |
| https://realpython.com/lessons/find-pattern-matches-python/ |
| Overview | https://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 Policy | https://realpython.com/privacy-policy/ |
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover