Wrap Up Finding Substrings With Regex
To learn more about regular expressions in Python, check out:
00:00
Okay, so this was a quick overview of working with regular expressions to find substrings with conditions in Python, and you do that with the re
module that you need to import from the standard library. And then we looked at three functions from the re
module.
00:16
Specifically, those were re.search()
. That gives you back a Match
object of the first regex pattern match in your strings, so starting from the beginning of the string that you’re searching in, the first one that matches is going to get returned.
00:29
Then you looked at .findall()
, which gives you a list of strings of all the pattern matches in the whole string that you’ve searched in. And I put a little asterisk (*
) here because like you saw before, if you do have more than one capturing group, then .findall()
doesn’t return a list of strings, but a list of tuples, and the tuples contain the strings representing each of the capturing groups.
00:52
And then finally, you also took a look at re.finditer()
, which gives you back an iterator that yields all the pattern matches as Match
objects.
01:00 So this gives you a lot of flexibility to work more with all of the substrings that match the pattern that you found inside of the text that you’re searching in. And we’ll keep it at that.
01:12 If you want to learn more about regular expressions in Python, then we do have a couple of resources on the site. There’s a video course and a text about Regular Expressions and Building Regexes in Python. There’s another, part-two regular expressions tutorial as well.
01:28 And then if you want to maybe replace strings, not just identify them, but then also do something with them, then you can check out How to Replace a String in Python.
01:39 That’s all about finding substrings with conditions using regex. In the next part of the course, you’re going to look at how you can find substrings in a panas DataFrame.
Become a Member to join the conversation.