import re
regex = re.compile(r"(https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)", flags=re.MULTILINE)
test_str = ("Welcome to RegExr 0.3b, an intuitive tool for learning, writing, and testing Regular Expressions. Key features include: \n\n"
"* real time results: shows results as you type \n"
"* code hinting: roll over your expression to see info on specific elements \n"
"* detailed results: roll over a match to see details & view group info below \n"
"* built in regex guide: double click entries to insert them into your expression \n"
"* online & desktop: regexr.com or download the desktop version for Mac, Windows, or Linux \n"
"* save your expressions: My Saved expressions are saved locally \n"
"* search Community expressions and add your own \n"
"* create Share Links to send your expressions to co-workers or link to them on Twitter or your blog [ex. http://RegExr.com?2rjl6] \n\n"
"Built by gskinner.com with Flex 3 [adobe.com/go/flex] and Spelling Plus Library for text highlighting [gskinner.com/products/spl].\n\n"
"https://google.com\n\n\n"
"https:google.com\n\n"
"www.cool.com.au\n\n"
"http://www.cool.com.au\n\n"
"http://www.cool.com.au/ersdfs\n\n"
"http://www.cool.com.au/ersdfs?dfd=dfgd@s=1\n\n"
"http://www.cool.com:81/index.html")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
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