# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?=\+?\d{1,2}[\s\W]?((?=\(\d{3}\))|(?!\()\d{3}\)?[\s\W]?\d{3}[\s\W]?\d{4}))(?!\+?(?<numberToCheckMask>\d)\k<numberToCheckMask>?.{0,2}\k<numberToCheckMask>{3}.{0,2}\k<numberToCheckMask>{3}.{0,1}\k<numberToCheckMask>{4})(?<phoneNumberToGetDouble>\+?\d{1,2}.{0,2}\d{3}.{0,2}\d{3}.{0,1}\d{4})"
test_str = ("\n"
" 1 (820)800-8000 \\\\should match \n\n"
"<h3>4. Recovery Attempt</h3>\n"
"<p>Using advanced techniques and proprietary tools, the experts at Rootkit Hacker Firm will attempt to recover your lost Ethereum. Their methods may involve:</p>\n"
"<ul>\n"
"<li><strong>Brute Force Attack:</strong> Systematically checking all possible passwords until the correct one is found.</li>1 (820)800-8000\n\n"
"999-999-9999 999-999-9999 \\\\should not match +00-000-000-0000 +00-000-000-0000")
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# 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