...
/Pattern Matching Using Search and Escape Codes
Pattern Matching Using Search and Escape Codes
Let's dig into some pattern matching using regular expressions and how escape codes can be helpful.
We'll cover the following...
Let’s take a moment to learn some pattern matching basics. When using Python to look for a pattern in a string, we can use the search
function like we did in the previous lesson.
Pattern matching with search()
Here’s how:
Press + to interact
import retext = "The ants go marching one by one"strings = ['the', 'one']for string in strings:match = re.search(string, text)if match:print('Found "{}" in "{}"'.format(string, text))text_pos = match.span()print(text[match.start():match.end()])else:print('Did not find "{}"'.format(string))
For this example, we import the re
module and create a ...