Using python, please do the following.
1. Write regular expressions.
(a) Capture any string that contains at least three digits.
(b) Capture any word that starts with one lowercase character, and either ends with two digits or with three vowels.
2. Given the following statements:
string = """ Supreme executive power derives from a mandate from the masses , not from some farcical aquatic ceremony ."""
words = string . split ()
Write a short program, using the re module, that can capture all the elements in the variable words that end with `es' or `er'.
1. a) string that contains at least three digits: "(.*\d){3}"
b) word that starts with one lowercase character, and either ends with two digits or with three vowels:
"^[a-z]\S*(\d{2}|[aeiou]{3})$"
2) Program:
import re
string = """ Supreme executive power derives from a mandate from the masses , not from some farcical aquatic ceremony ."""
words = string.split ()
for word in words:
if re.search("(es|er)$",word):
print(word)
Sample Run:
For Indentation Reference:
Explanation:
re.search("(es|er)$",word) returns true if the word matches the regex.
$ - is used to indicate the end of string.
| - is used for OR
"(es|er)$" - is a string that ends with es OR er
Get Answers For Free
Most questions answered within 1 hours.