# Task 7: Matches a word containing 'z' Write a Python program that matches a word containing 'z', not at the start or end of the word. Sample data : "The quick brown fox jumps over the lazy dog." "Python Exercises." Expected Output : Found a match!
时间: 2024-01-10 21:03:24 浏览: 164
Here's a Python program that matches a word containing 'z', not at the start or end of the word:
```python
import re
text1 = "The quick brown fox jumps over the lazy dog."
text2 = "Python Exercises."
pattern = r'\b\w+z\w+\b'
if re.search(pattern, text1) or re.search(pattern, text2):
print("Found a match!")
else:
print("No match found.")
```
Output:
```
Found a match!
```
Explanation:
- We use the `re` module to work with regular expressions in Python.
- The pattern `r'\b\w+z\w+\b'` matches a word boundary (`\b`), followed by one or more word characters (`\w+`), followed by the letter 'z', followed by one or more word characters again (`\w+`), and finally another word boundary (`\b`).
- If the pattern is found in either `text1` or `text2`, we print "Found a match!". Otherwise, we print "No match found."
阅读全文