Match a Substring Using re.search()
00:00 Let’s think about one of the maybe issues that you encountered early on. While you did lowercase all of the text to make it a bit more general to search, you might have noticed that still, given to the fact that every character is just a character for Python, you might get results that you’re not expecting.
00:20 So earlier, you were looking for how often is secret in there, and the count gave you four times, even though one of those is actually a different word—that’s secretly. Still, the word secret is in there, so the count makes complete sense according to Python.
And here I’m adding the
r before the string to create a raw string. This is normal Python and doesn’t have anything to do with regex. In a raw string, Python treats the string literally without interpreting any special characters.
If the combination of these two characters is in a normal Python string, then it stands for a newline character. If it’s in a raw string, then Python treats it as two separate characters:
n. Such raw strings are useful for writing regex patterns because the backslash character has a special meaning also in regular expressions. And if you use a raw string, then you can avoid having to double-escape the backslash character. For now, the quick takeaway is that prefixing your regex pattern with
r can make writing it a bit less complicated, and you can use every bit of reduced complication that you can get with regex patterns.
And in regex, a word character means any letter, digit, or underscore, which means that it’s going match this
l here that comes after, but it won’t match a whitespace that would come after some or a dot, for that matter. So in this case, it’s going to match the
l, but you want to get the full word, so you need more than one word character, and you can do that with by adding the quantifier plus (
+) at the end.
So this regular expression pattern is going to find the word
"secretly" here, but it’s more flexible than that. The same pattern would also match other words such as
"secretary", or even
Become a Member to join the conversation.