Text preprocessing is one of the most important tasks in Natural Language Processing. You may want to extract number from a string. Writing a manual script for such processing task requires a lot of effort and at most times it is prone to errors. Keeping in view the importance of these preprocessing tasks, the concept of Regular Expression have been developed in different programming languages in order to ease these text processing tasks.
To implement Regular Expression, the python re package can be used and to be used it can be easily imported like any other inbuilt python module.
Steps for converting a 10-digit phone number to its corresponding US number format:
- Import the python re package.
- Write a function that takes the phone number to be formatted as argument and processes it.
- Now simply call the function and pass the value.
Example:
Python3
import re
def convert_phone_number(phone):
num = re.sub(r '(?<!\S)(\d{3})-' , r '(\1) ' , phone)
return num
print (convert_phone_number( "Call geek 321-963-0612" ))
|
Output:
Call geek (321) 963-0612
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 Dec, 2020
Like Article
Save Article