Source: https://yellorn.com/programming/regex-split-the-phone-numbers
There is a list of phone numbers that need the attention of a text processing expert. As an expert in regular expressions, you are being roped in for the task. A phone number directory can reveal a lot such as country codes and local area codes. The only constraint is that one should know how to process it correctly.
A Phone number is of the following format
[Country code]-[Local Area Code]-[Number]
There might either be a ‘-’ ( ASCII value 45), or a ’ ’ ( space, ASCII value 32) between the segments
Where the country and local area codes can have 1-3 numerals each and the number section can have 4-10 numerals each.
And so, if we tried to apply the regular expression with groups on this phone number: 1-425-9854706
We’d get:
Group 1 = 1
Group 2 = 425
Group 3 = 9854706
You will be provided a list of N phone numbers that conform to the pattern described above. Your task is to split it into the country code, local area code, and number.
Input Format
N, where N is the number of tests.
This will be followed by N lines containing N phone numbers in the format specified above.
Constraints
1 <= N <= 20
There might either be a hyphen, or a space between the segments
The country and local area codes can have 1-3 numerals each and the number section can have 4-10 numerals each.
Output Format
Your output will contain N lines.
CountryCode=[Country Code],LocalAreaCode=[Local Area Code],Number=[Number]
Sample Input
2
1 877 2638277
91-011-23413627
Sample Output
CountryCode=1,LocalAreaCode=877,Number=2638277
CountryCode=91,LocalAreaCode=011,Number=23413627
Testcase
Input
6
148-809-2561957985
188-547-5864327428
891-454-9195497623
648-42-380306686
824-417-6460145493
489-16-9839392781
Output
CountryCode=148,LocalAreaCode=809,Number=2561957985
CountryCode=188,LocalAreaCode=547,Number=5864327428
CountryCode=891,LocalAreaCode=454,Number=9195497623
CountryCode=648,LocalAreaCode=42,Number=380306686
CountryCode=824,LocalAreaCode=417,Number=6460145493
CountryCode=489,LocalAreaCode=16,Number=9839392781
Solution
import sys
import re
pattern = r"(\d+)\D(\d+)\D(\d+)"
for i in range(int(input())):
text = sys.stdin.readline()
result = re.search(pattern, text)
if result:
print(f"CountryCode={result.group(1)},LocalAreaCode={result.group(2)},Number={result.group(3)}")
Comments
Post a Comment