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 split phone number and name from the text string using regex

import re
tenant_details = "Tasha Robinson (123) 4178-6875"
name = re.search("(?<=[a-zA-Z])(?=\d)",tenant_details)
print(name)

Required solution:

Name: Tasha Robinson
Phone No: (123) 4178-6875

The name can be full name or first name and phone number can be in any format like +1 854 545454 ..

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 :

You can specify that phone number consists only from characters +()\d- and spaces. Then you can use:

import re

tenant_details = "Tasha Robinson (123) 4178-6875"

name, phone = re.match(r"(.*?)\s([)(+\d -]+)$", tenant_details).groups()
print(f"{name=}")
print(f"{phone=}")

Prints:

name='Tasha Robinson'
phone='(123) 4178-6875'
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