# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"((?<=[^a-zA-Z0-9])(?:https?\:\/\/|[a-zA-Z0-9]{1,}\.{1}|\b)(?:\w{1,}\.{1}){1,5}(?:com|org|edu|gov|uk|net|ca|de|jp|fr|au|us|ru|ch|it|nl|se|no|es|mil|iq|io|ac|ly|sm){1}(?:\/[a-zA-Z0-9]{1,})*)"
test_str = ("== Good ==\n\n"
"https://facebook.com/marko/polo\n"
"https://facebook.com/marko/pol2o\n"
"www.moshe.io\n"
"http://marko.polo.com\n"
"subdomain.pizza.com\n"
"bitly.com/14awOx4\n\n"
"== Good in random text\n"
"Dude, I'm telling you, every other https://facebook.com/marko/polo URL regex just so sucked. it even crashed https://facebook.com/marko/pol2o. In one case + there are case where www.moshe.io would be cought but this one works likes charm http://marko.polo.com exactly what I needed // // look it doesn't catch subdomain.pizza.com anyhthing else ! it's totally amazing !\n"
"bitly.com/fdsafdf\n\n\n"
"== Bad ==\n\n"
"https://www.........more..........no/com\n"
"4.5x10.9\n"
"etc..!\n"
"me.you\n"
"you.them\n")
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