Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading