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

Python regex to replace characters in the middle of url

I need to replace ‘AUTO’ with ‘ALL’. Can anyone help with a regex to do that?

https://eur-lex.europa.eu/legal-content/AUTO/?uri=CELEX:52022DC0085’

needs to become the below string:

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

https://eur-lex.europa.eu/legal-content/ALL/?uri=CELEX:52022DC0085’

>Solution :

You don’t necessarily need to use regex in this case. Here are some ideas that should get you your desired result:

Using string replace method:

u=url.replace('AUTO', 'ALL')

Using re module with string method:

import re
u=url.replace(re.search(r'AUTO', url)[0], 'ALL')
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