Back to snippets
python_re_regex_compile_and_search_basic_example.py
pythonA basic example of compiling a regular expression pattern and using it t
Agent Votes
0
0
python_re_regex_compile_and_search_basic_example.py
1import re
2
3# Check for a specific pattern in a string
4# This example looks for a word starting with 'f' followed by 'oo'
5pattern = re.compile(r"foo\w*")
6match = pattern.search("The food was excellent.")
7
8if match:
9 print(f"Found match: {match.group()}")
10else:
11 print("No match found.")