# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\b((bc|tb)(0([ac-hj-np-z02-9]{39}|[ac-hj-np-z02-9]{59})|1[ac-hj-np-z02-9]{8,87})|([13]|[mn2])[a-km-zA-HJ-NP-Z1-9]{25,39})\b\s"
test_str = ("BTC Address: 1JHwenDp9A98XdjfYkHKyiE3R99Q72K9X4 \n"
"BTC Address: 1Unoc4af6gCq3xzdDFmGLpq18jbTW1nZD\n"
"BTC Address: 1A8Ad7VbWDqwmRY6nSHtFcTqfW2XioXNmj\n"
"BTC Address: 12CZYvgNZ2ze3fGPFzgbSCELBJ6zzp2cWc\n"
"BTC Address: 17drmHLZMsCRWz48RchWfrz9Chx1osLe67\n\n"
"Receiving Bitcoin Address: 15LZALXitpbkK6m2QcbeQp6McqMvgeTnY8\n"
"Receiving Bitcoin Address: 1MAFzYQhm6msF2Dxo3Nbox7i61XvgQ7og5\n\n"
"--------------------------------------------------------\n"
"Other possible BitCoin test cases I added:\n"
"--------------------------------------------------------\n"
"- What if text comes before and/or after on same line? Or doesn't contain BitCoin/BTC/etc. anywhere (or anywhere close to the address)?\n"
" Send BitCoin payments here 1MAFzYQhm6msF2Dxo3Nbox7i61XvgQ7og5\n"
" 1MAFzYQhm6msF2Dxo3Nbox7i61XvgQ7og5 to keep your secrets safe.\n"
" Send payments here 1MAFzYQhm6msF2Dxo3Nbox7i61XvgQ7og5 to keep your secrets safe.\n\n"
"- Standalone address:\n"
" 1Dvd7Wb72JBTbAcfTrxSJCZZuf4tsT8V72\n\n"
"--------------------------------------------------------\n"
"Redacted Body content generating FPs from spam emails:\n"
"--------------------------------------------------------\n"
"src=3D\"https://example.com/blah=3D2159024400&t=3DXWP9YVkAYwkmif9RgKeoPhw2b1zdMnMzXZSGRD_Oxkk\"\n\n"
"\"cursor:pointer;color:#6A6C6D;-webkit-text-size-blahutm_campaign%253Drdboards%2526e_t%253Dd5c2deeaae5c4a8b8d2bff4d0f87ecdd%2526utm_cont=blah\n\n"
"src=3D\"https://example.com/blah/74/328e74997261d5228886aab1a2da6874.jpg\" \n\n"
"src=3D\"https://example.com/blah-1c779f59948fc5be8a461a4da8d938aa.jpg\"\n\n"
"href=3D\"https://example.com/blah-0ff3169b28a6e17ae8a369a3161734c1?alert_=id=blah")
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