Using Python write regular expressions to capture the follow
Using Python, write regular expressions to capture the following sets of strings and a function that will match a regex against a string to evaluate it.
A) all strings that start at the beginning of the line with an integer and that end at the end of the line with a word;
B) all strings that have both the word “like” and the word “duck” in them (but not, e.g., words such as “likes” that merely contain the word “like”;
C) write a pattern that places the first word of a sentence in the complete words of Shakespeare in a file. Deal with punctuation.
I started with
import re
def match(pattern, string):
result = False
match = re.match(pattern,string,re.IGNORECASE)
if match:
result = True
print(\"Testing if {} will match {}. Result: {}\".format(pattern,string, result))
return match
Solution
1) \"[0-9]+[ a-zA-Z]+[A-Za-z]+\" this regular expression will support integers at the beginning, than anything in between including char, numbers, spaces and then word at the end
2) \"(([ a-zA-Z0-9]*[^a-zA-Z](duck)+[^a-zA-Z][ a-zA-Z0-9]*[^a-zA-Z](like)+[^a-zA-Z][ a-zA-Z0-9]*)|([ a-zA-Z0-9]*[^a-zA-Z](like)+[^a-zA-Z][ a-zA-Z0-9]*[^a-zA-Z](duck)+[^a-zA-Z][ a-zA-Z0-9]*))+\" this regular expression is scarry as hell I know but thats how RE suppose to look like. I need to handle all the failing cases thats why this long
I have not answered the third query, it is little ambigious what you are trying to ask, please elaborate a little or maybe ask a question again with that query only.
Here is a url for demo code in case code formatting gets destroyed in this text editor: https://www.hastebin.com/kekiwuxiji.md
----------------
import re
def match(pattern, string):
result = False
match = re.match(pattern,string,re.IGNORECASE)
if match:
result = True
print(\"Testing if {} will match {}. Result: {}\".format(pattern,string, result))
return match
match(\"(([ a-zA-Z0-9]*[^a-zA-Z](duck)+[^a-zA-Z][ a-zA-Z0-9]*[^a-zA-Z](like)+[^a-zA-Z][ a-zA-Z0-9]*)|([ a-zA-Z0-9]*[^a-zA-Z](like)+[^a-zA-Z][ a-zA-Z0-9]*[^a-zA-Z](duck)+[^a-zA-Z][ a-zA-Z0-9]*))+\",\"33 like asd ducks aa22\")
-------------------------

