Question

Using python, please do the following. 1. Write regular expressions. (a) Capture any string that contains...

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'.

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1.Write a function which takes a string that contains two words separated by any amount of...
1.Write a function which takes a string that contains two words separated by any amount of whitespace, and returns a string in which the words are swapped around and the whitespace is preserved. Hint: use regular expressions where \s detects the whitespaces in a string. Example: given "Hello     World", return "World         Hello" 2.Pandas exercises: Write a python program using Pandas to create and display a one-dimensional array-like object containing an array of data. Write a python program using Pandas to...
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter...
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter feeds. At a high level, your program is going to perform the following: Read in two files containing twitter feeds. Merge the twitter feeds in reverse chronological order (most recent first). Write the merged feeds to an output file. Provide some basic summary information about the files. The names of the files will be passed in to your program via command line arguments. Use...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output. Here is the starter outline for the homework: d. def count_words(text): """ Count the number of words in text """ return 0 e. def words_per_sentence(text): return 0.0 f. def word_count(text, punctuation=".,?;"): """ Return a dictionary of word:count...