I was trying to remove middle initials from a list of names so that they all conformed to FirstName space LastName. so I tried writing a regular expression that I could then use to match list items that had a middle initial then replace it with '' an empty space.
Here is my code:
import re
list = ['John A Appleseed', 'Bonnie N Clyde', 'Joseph B Barthalomew', 'John Smith']
mid_name = re.compile(r'\s+[A-Z]\s+')
for idx, names in enumerate(list):
if re.match(mid_name, names) is not None:
list[idx] = mid_name.sub('', names)
print(list)
My results were:
['John A Appleseed', 'Bonnie N Clyde', 'Joseph B Barthalomew', 'John Smith']
I then changed my regular expression to:
mid_name = re.compile(r'\w+\s+[A-Z]\s+\w+')
And get:
['', '', '', 'John Smith']
Then changed the regular expression to:
mid_name = re.compile(r'[A-Z]\s+')
because I realized I wanted to keep at least one of those spaces anyway, but still got:
['John A Appleseed', 'Bonnie N Clyde', 'Joseph B Barthalomew', 'John Smith']
What is it that I'm missing? I feel like I'm really close to my solution, but it's alluding me. Any assistance would be appreciated.