Question

In: Computer Science

What is a potential solution for an Array that contains these lines to find a regex...

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

Solutions

Expert Solution

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

Related Solutions

Find the vector and parametric equations for the plane. The plane that contains the lines r1(t)...
Find the vector and parametric equations for the plane. The plane that contains the lines r1(t) = <6, 8, 8,> + t<-2, 9, 6> and r2 = <6, 8, 8> + t<5, 1, 7>.
A.) Find the pH of a solution that contains 3.25 g of H2SO4 dissolved in 2.75...
A.) Find the pH of a solution that contains 3.25 g of H2SO4 dissolved in 2.75 liters of solution. B.) If the pH is 9.85, what is the concentration of the aluminum hydroxide solution?
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
1. A solution of AgNO3 contains 30 g/liter. What volume of this solution is needed to...
1. A solution of AgNO3 contains 30 g/liter. What volume of this solution is needed to react with 0.60 g of 65% pure AlCl3?
(1) What is a potential advantage of array based replication over and above hypervisor and application...
(1) What is a potential advantage of array based replication over and above hypervisor and application based replication? A. More tightly integrated with applications B. Requires less hardware C. Requires smaller replication links D. Offloads replication work from the host (2) What kind of scenarios does storage replication assist in recovery from? A. Replication data corruption B. Virus C. User accidentally deleting data D. Power failures and natural disasters (3) Which of the following is being described here: This kind...
Seasons Re-Visited Write a program that contains an array of strings in main(). Your array of...
Seasons Re-Visited Write a program that contains an array of strings in main(). Your array of strings will contain the seasons, “Winter”, “Spring”, “Summer”, and “Fall”. Your program will then contain a function that will use your seasons array to display a menu and ask the user to choose their favorite season and return that value to the main() function. Your function must include a data validation loop. The user must enter in a 1 for winter, a 2 for...
Sum Bibi have an X × X array. Each array contains an integer a, and Bibi...
Sum Bibi have an X × X array. Each array contains an integer a, and Bibi is curious about how much is the sum of all integers in each column. Format Input: The input consists of an integer T. The next T line contains X integers followed by an X × X array. Each array contains integers a. Format Output The output contains ”Case #T:” followed by X integers separated by spaces that indicate the sum of all the numbers...
I want to scan in 4 ints and the next 3 lines into an array of...
I want to scan in 4 ints and the next 3 lines into an array of a text file in java. Ex 1 2 3 adacdac acddddd acdadcd 1,2,3 would be stored into seperate int values and the next 3 strings would be stored into an array
What differential equation is the one-dimensional potential equation? What is the form of the solution of...
What differential equation is the one-dimensional potential equation? What is the form of the solution of the one-dimensional Dirichlet problem? The one-dimensional Neumann problem?
Potential solution 1: Describe the potential solution for environmental threats to mutualism, commensalism and parasitism. Give...
Potential solution 1: Describe the potential solution for environmental threats to mutualism, commensalism and parasitism. Give details as to who is in charge of this potential solution, what they are trying to do, and what their goals are.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT