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

editing the first word in match, regex

I have a regex problem. In found matches (these are lines from a text file) I want to edit them so I can get shortened version of these lines. For example:

matches=[Dog Alex, Dog Chriss, Cat Susan, Lizard Bob, and so on]

From this I want to get:

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

new_version=[D.Alex, D.Chriss, C.Susan, and so on]

So I wrote this code:

for match in matches:
    mpattern=re.compile(r'^\w (.+)')
    match=re.sub(mpattern, r'[A-Z]\. (.+)', match)
    list_of_descriptions.append(match)

but it doesn’t work correctly 🙁

I need the program to find the first word (which could begin with any letter) and then shorten it to the first letter and add a dot. Could someone help me, please? I’m using Python 3.7.9.

>Solution :

The regex should capture the first letter, but should allow for more letters in the first word. On the other hand, as you’re not planning to change anything to the second word, there is no need to include it in the match.

Side note: when you compile your regex, you can call the sub method on that object, instead of on re.

So:

mpattern = re.compile(r'^(\w)\w* ')
list_of_descriptions = [mpattern.sub(r'\1.', match) for match in matches]
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