# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^(?:(?:\+|00)33[\s.-]{0,3}(?:\(0\)[\s.-]{0,3})?|0)[1-9](?:(?:[\s.-]?\d{2}){4}|\d{2}(?:[\s.-]?\d{3}){2})$"
test_str = ("French Phone Number Validation.\n"
"Accept several kind of format, more or less usual. And refuse incorrect formats.\n"
" - Prefix: 0033 / +33 / (0) / 0\n"
" - Number: collapsed / group by 2 / group by 3\n"
" - Separator: space / dot / dash\n\n"
"~ Accepted ~\n"
"0123456789\n"
"01 23 45 67 89\n"
"01.23.45.67.89\n"
"0123 45.67.89\n"
"0033 123-456-789\n"
"0033(0)123456789\n"
"+33-1.23.45.67.89\n"
"+33 - 123 456 789\n"
"+33(0) 123 456 789\n"
"+33 (0)123 45 67 89\n"
"+33 (0)1 2345-6789\n"
"+33(0) - 123456789\n\n"
"~ Rejected ~\n"
"012345678\n"
"01234567890\n"
"0+123456789\n"
"(0)123456789\n"
"01 23 45 67 89\n\n"
"~ Accepted but strange ~\n"
"+33.-.123456789\n"
"+33.-.(0)-.-123456789\n"
"01 23.45-67.89\n")
subst = "false"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html