import re
regex = re.compile(r"a")
test_str = ("STAT 545 REGULAR EXPRESSION TEST BED\n"
"------------------------------------\n\n"
"abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"
"0123456789 +-.,!@#$%^&*();\\/|<>\"'\n"
"12345 -98.7 3.141 .6180 9,000 +42\n\n"
"Twas brillig, and the slithy toves\n"
" Did gyre and gimble in the wabe;\n"
"All mimsy were the borogoves,\n"
" And the mome raths outgrabe.\n\n"
"3317 Route 202 \n"
"Long Branch, NJ 07740\n\n"
"9303 Route 1 \n"
"Sun City, AZ 85351\n\n\n"
"CHALLENGES\n"
"-------------------------------------\n\n"
"RULES\n"
"1. Match ONLY the target elements (check top of document as well!)\n"
"2. Must be a separate match for each discrete element\n"
"3. THERE ARE CANDY PRIZES\n\n"
"1. DNA:\n"
"TCCGTATCAAGATCCTCTTAATAAGCCCCCGTCACTGTTGGTTGTAGAGCCCAGGACGGGTTGGCCAGATGTGCGACTATATCGCTTAGTGGCTCTTGGGCC\n\n"
"GATAGCTTCTTACCGGTGCGCCTCCGTACGCAGTACGATCGCACGCCCCATGAGAACGATAGGTAAACCTGGTGTCCTGTGAGCGACAAAAGCTTAAATGG\n\n"
"2. SMILIES:\n"
":D :) :o :P :/\n\n"
"3. EMAIL ADDRESSES:\n"
"foo@demo.net \n"
"bar.ba@test.co.uk\n\n"
"4. HTML Tags:\n"
"<p>The <a class=\"APILink\" target=\"_blank\" href=\"https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html\"><code>Pattern</code></a> API contains a number of useful <i>predefined character classes</i>, which offer convenient shorthands for commonly used regular expressions:</p>\n"
"<p><a name=\"CHART\" id=\"CHART\"></a></p>\n\n"
"5. PHONE NUMBERS:\n"
"555.123.4567 \n"
"+1-800-555-2468 \n"
"+49 7743 9901\n\n"
"6. URLs:\n"
"www.demo.com \n"
"http://foo.co.uk/\n\n"
"7. MACHO MAN RANDY SAVAGE QUOTATIONS:\n\n"
"“History beckons the Nacho Man.”\n\n"
"“Hulkamania is like a single grain of sand in the Sahara desert that is Nacho Madness.”\n\n"
"\"Snap into a Slim Jim!\"\n\n"
"9. CITATIONS:\n"
"We based our calculations on previously published methods (Nadeu 2006).\n\n"
"We also used a support vector machine, which is a classification method that is able to deal with highly variable data (Camps-Valls et al. 2004). \n\n"
"The population of this species...where the presence and transmission of introduced avian malaria (Plasmodium relictum) is low (Eggert et al. 2008, Dziel et al. 2010)")
match = regex.search(test_str)
if match:
print(f"Match 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