Question

Use the Python “re” module to do the following: 1. Write a regular expression pattern that matche...

Use the Python “re” module to do the following:
1. Write a regular expression pattern that matches string “March 1, 2019, Mar 1, 2019, March First, 2019, March First, 19”. No credit for not using special characters: “*,+,?, | and etc” to do the matching.
2. Write a regular expression pattern that matches strings representing trains. A single letter stands for each kind of car in a train: Engine, Caboose, Boxcar, Passenger car, and Dining car. There are four rules specifying how to form trains.
1. One or more Engines appear at the front; one Caboose at the end.
2. Boxcars always come in pairs: BB, BBBB, etc.
3. There cannot be more than four Passenger cars in a series.
4. One Dining car must follow each series of passenger cars.
These cars cannot appear anywhere other than these locations.
Here are some legal and illegal examples.
EC Legal: the smallest train
EEEPPDBBPDBBBBC Legal : simple train showing all the cars
EEBB Illegal: no caboose (everything else OK)
EBBBC Illegal: three boxcars in a row
EEPPPPPDBBC Illegal: more than four passenger cars in a row
EEPPBBC Illegal: no dining car after passenger cars
EEBBDC Illegal: dining car after box car
What to turn in:
1. Your Python expressions or module.

2. Paste your test results.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.

solution

code

#import the re module import re # regular expression regex ([a-zA-2) d+) matchdata= re.search (regex, March 1, 2019, Mar 1output

string matches: March

copyable code

#import the re module

import re

# regular expression

regex = r"([a-zA-Z]+) (\d+)"

matchdata = re.search(regex, "March 1, 2019, Mar 1, 2019, March First, 2019, March First, 19")

if matchdata != None:

    

     # display the month

     print "string matches: %s" % (matchdata.group(1))

    

#does not match the regular expression string

else:

     print "The regex pattern does not match."

2.

coding

import re #check the valid train process def isValid Train (train1): patternl - rE+ (((P PPI PPP PPPP) D) (BB))CS if re.matc

output

RESTART: C:\Users\M.MANOGAR Desktop match.py Train,EC, is valid:, True) (TrainEEEPPDBBPDBBBBC,is valid: , True) (

copyable code

import re

#check the valid train process

def isValid_Train(train1):

    pattern1 = r'^E+(((P|PP|PPP|PPPP)D)*(BB)*)*C$'

    if re.match(pattern1, train1):

        return True

    return False

#print the reult

def check_Print_Train(train1):

    print("Train", train1, "is valid:", isValid_Train(train1))

check_Print_Train("EC")

check_Print_Train("EEEPPDBBPDBBBBC")

check_Print_Train("EEBB")

check_Print_Train("EBBBC")

check_Print_Train("EEPPPPPPDBBC")

check_Print_Train("EEPPBBC")

check_Print_Train("EEBBDC")

Add a comment
Know the answer?
Add Answer to:
Use the Python “re” module to do the following: 1. Write a regular expression pattern that matche...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Preliminaries For this lab you will be working with regular expressions in Python. Various functions for...

    Preliminaries For this lab you will be working with regular expressions in Python. Various functions for working with regular expressions are available in the re module. Fortunately, Python makes it pretty easy to check if a string matches a particular pattern. At the top of the file we must import the re module: import re Then we can use the search() function to test whether a string matches a pattern. In the example below, the regular expression has been saved...

  • Write a PHP regular expression pattern that matches a string that satisfies the following description: The...

    Write a PHP regular expression pattern that matches a string that satisfies the following description: The string must begin with the (uppercase) letter A. Any three alphanumeric characters must follow. After these, the letter B (uppercase or lowercase) must be repeated one or more times, and the string must end with two digits.

  • please use python and provide run result, thank you! click on pic to make it bigger...

    please use python and provide run result, thank you! click on pic to make it bigger For this assignment you will have to investigate the use of the Python random library's random generator function, random.randrange(stop), randrange produces a random integer in the range of 0 to stop-1. You will need to import random at the top of your program. You can find this in the text or using the online resources given in the lectures A Slot Machine Simulation Understand...

  • 1. According to the paper, what does lactate dehydrogenase (LDH) do and what does it allow...

    1. According to the paper, what does lactate dehydrogenase (LDH) do and what does it allow to happen within the myofiber? (5 points) 2. According to the paper, what is the major disadvantage of relying on glycolysis during high-intensity exercise? (5 points) 3. Using Figure 1 in the paper, briefly describe the different sources of ATP production at 50% versus 90% AND explain whether you believe this depiction of ATP production applies to a Type IIX myofiber in a human....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT