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

How to extract text using regex for various text types

import re
a = "Office: (000) 987-9728, Mobile: (000) 312-9725"
b = "Mobile: (000) 312-8750, Office: (000) 987-9728"
c = "Mobile: (000) 312-8750"
d = "Office: (000) 987-9728"
search_office_phone = re.search("Office:\s?(.*),|Office:\s?(.*)",a)
if search_office_phone:
    office_number = search_office_phone.group(1)
    print(office_number)

Required solution: (000) 987-9728, (000) 312-9725

This are the various text that can be formed, but using regex I need to got the solution above.

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

>Solution :

[re.sub(r"Office: |Mobile: ", "", s) for s in [a, b, c, d]]
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