-
-
Save Codehunter-py/e138c804067fab7f9740e6507891dc4e to your computer and use it in GitHub Desktop.
import re | |
def convert_phone_number(phone): | |
result = re.sub(r"\b(\d{3})-(\d{3})-(\d{4})\b",r"(\1) \2-\3", phone) | |
return result | |
print(convert_phone_number("My number is 212-345-9999.")) # My number is (212) 345-9999. | |
print(convert_phone_number("Please call 888-555-1234")) # Please call (888) 555-1234 | |
print(convert_phone_number("123-123-12345")) # 123-123-12345 | |
print(convert_phone_number("Phone number of Buckingham Palace is +44 303 123 7300")) # Phone number of Buckingham Palace is +44 303 123 7300 |
Hi,
\b
Matches the empty string, but only at the beginning or end of a word. A word is defined as a sequence of word characters. Note that formally, \b is defined as the boundary between a \w and a \W character (or vice versa), or between \w and the beginning/end of the string. This means that r'\bfoo\b' matches 'foo', 'foo.', '(foo)', 'bar foo baz' but not 'foobar' or 'foo3'.
Hi,
\b Matches the empty string, but only at the beginning or end of a word. A word is defined as a sequence of word characters. Note that formally, \b is defined as the boundary between a \w and a \W character (or vice versa), or between \w and the beginning/end of the string. This means that r'\bfoo\b' matches 'foo', 'foo.', '(foo)', 'bar foo baz' but not 'foobar' or 'foo3'.
thank you for both the code 👍 and clear explanation ~
if i remove both \b it also includes 3rd one but why is that, even if i specified 3rd group's length is 4