Search code examples
pythonregexloopsreplacesubstring

iterate over strings and move pattern to start of each list element


I have a list of names that have the title Dr. in the wrong place.

Therefore i would like to

  • loop over the list elements to replace either Dr., or Dr. with
  • while also adding/moving Dr. to the start of the corresponding strings.

my result is rather disappointing. Is re.sub() even the right approach?

names = ['Johnson, Dr., PWE', 'Peterson, FDR', 'Gaber, Dr. GTZ']
for idx, item in enumerate(names):
    names[idx] = re.sub(r' Dr.(,)? ', ' Dr. ', item)
print(names)
['Johnson, Dr. PWE', 'Peterson, FDR', 'Gaber, Dr. GTZ']

desired_names = ['Dr. Johnson, PWE', 'Peterson, FDR', 'Dr. Gaber, GTZ']

Solution

  • You can use 2 capture groups, and use those reverted in the replacement to get the right order.

    ([^,\n]+,\s*)(Dr\.),?\s*
    
    • ([^,\n]+,\s*) Capture any char except , or a newline in group 1, then match a comma and optional whitespace char
    • (Dr\.) Capture Dr. in group 2
    • ,?\s* Match an optional comma and whitespace chars

    Regex demo | Python demo

    Example

    import re
    names = ['Johnson, Dr., PWE', 'Peterson, FDR', 'Gaber, Dr. GTZ']
    for idx, item in enumerate(names):
        names[idx] = re.sub(r'([^,\n]+,\s*)(Dr\.),?\s*', r'\2 \1', item)
    print(names)
    

    Output

    ['Dr. Johnson, PWE', 'Peterson, FDR', 'Dr. Gaber, GTZ']