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...
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...
(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...
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?
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements....
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements. Start by creating 2 arrays that can each hold 10 integer values. Then, get values from the user and populate the 1st array. Next, populate the 2nd array with the values from the 1st array in reverse order. Then, average the corresponding elements in the 1st and 2nd arrays to populate a 3rd array (average the 1st element of the 1st array with the...
Write a program in MIPS to find the largest element of an array, the array size...
Write a program in MIPS to find the largest element of an array, the array size should be less than or equal to 10. Has to be extremely basic, cannot use stuff like move. Very basic. Here is what I already have and I am stuck. .data myarray: .word 0,0,0,0,0,0,0,0,0,0 invalid: .asciiz "Number is invalid, store a number in the array that is from 0-10.\n" large: .asciiz "The largest element is " colon: .asciiz " :\t" enter: .asciiz "Store a...
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT