In: Computer Science
What is a potential solution for an Array that contains these lines to find a regex solution first names only and a separate regex solution for last names? (PYTHON)
John Doe
John, E. Doe
John, E., Doe
John, Doe E.
John, Doe
Jo hn, Doe Jr. (first name is "Jo hn")
John, Doe III
John, E Doe
Expect OutPut (bolded - first name (1st regex), italicized - last name (2nd regex))
John Doe
John, E. Doe
John, E., Doe
John, Doe E.
John, Doe
Jo hn, Doe Jr. (first name is "Jo hn")
John, Doe III
John, E Doe
Below is a screen shot of the python program to check indentation. Comments are given on every line explaining the code. Below is the output of the program: Below is the code to copy: #CODE STARTS HERE----------------
import re #Test list li = ["John Doe","John, E. Doe","John, E., Doe","John, Doe E.","John, Doe", "Jo hn, Doe Jr.","John, Doe III","John, E Doe"] for x in li: #Searches for words with capital letter in the beginning of the sentence #and should end with either ',' or another word's capital letter y = re.search("(^[A-Z][a-z ]*),?[A-Z]*",x) #Finds all words with capital letter at the beginning and extracts the last #matching word in a input z = re.findall("([A-Z][a-z]{2,})",x) #Replace first name with bold font, and last name with underline font full_name = x.replace(y.group(1),'\033[1m'+y.group(1)+'\033[0m').\ replace(z[-1],'\033[4m'+z[-1]+"\033[0m") print(full_name) #Print resilt #CODE ENDS HERE------------------