# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^(The|A|An)"
test_str = ("A Great Calamity.\n"
"An Original idea and Time of the Day.\n"
"Alice in Wonderland and a and the Tools\n"
"The Runner and a Scam\n"
"The Boy and the Car and a Box\n"
"They Had a Dream\n"
"the dream is here\n\n\n"
"Try clicking the \"Run Match\" button (or F5) to see what\n"
"happens. After a successful match click in the results box\n"
"to highlight the matched text. Click the \"Replace\" button\n"
"using the \"Dates\" example. Notice in the results box that the\n"
"format of all the dates has been altered. Using the\n"
"\"Expression Library\" tab double-click another regular\n"
"expression and \"Run Match\".\n\n"
"Click \"Show the Builder\" (or Ctrl+D) to start designing your\n"
"own regular expressions; or enter them directly in the\n"
"regular expression box. Enter the text you want to search in\n"
"the sample text box.\n\n"
"Expand nodes in the \"Regex Analyzer\" to see a description\n"
"of the current regular expression. Right-click a node to edit\n"
"the expression.\n\n"
"--- Sample text ---\n"
"Comma Separated Values\n"
"a,b,c,d,e\n"
"Dog,Cat,Puppy\n\n"
"US phone numbers, (800) 325-3535, (650) 555 1212\n"
"US zip codes, 95076-1234, 90210-6473\n"
"Dates:12/25/02 9/11/2001 11/22/63 12/7/41\n\n"
"URLs:\n"
"http://www.usgs.gov\n"
"http://www.acl.lanl.gov/URI/archive/uri-archive.index.html\n"
"ftp://@host.com/\n"
"ftp://host.com/\n"
"ftp://foo:@host.com/\n"
"ftp://myname@host.dom/%2Fetc/motd\n"
"file://vms.host.edu/disk$user/my/notes/note12345.txt\n"
"prospero://host.dom//pros/name\n\n"
"IP addresses:\n"
"123.54.63.0\n"
"255.257.0.1 -> This one has an illegal number\n"
"0.255.255.12\n\n"
"UK Postal Codes:\n"
"M1 1AA \n"
"M60 1NW \n"
"ABD3 7BB -> This is an illegal format\n"
"M1 1CA -> This has illegal letters (CIKMOV) in the second half\n"
"W1A 1HQ \n\n"
"Canadian Postal Codes:\n"
"A1A 1A1\n"
"B6C 8X7\n"
"5 Shoreham Drive, Downsview, Ontario, M3N 1S4\n\n"
"Netherlands Postal Codes:\n"
"1234 ZA\n"
"5678 KL\n"
"0123 AZ -> Illegal number, must be 1000-9999\n"
"5432 ABQ -> Too many characters\n\n"
" \n"
"Here is some text as bait for the multiple captures example:\n"
"XYZAbcAbcAbcXYZAbcAb\n"
"acacacac\n\n"
" Paris\n"
" in the\n"
"the spring\n\n"
"{a[b(c)d]e}\n\n"
"Email:\n"
"esupport@ultrapico.com\n"
"maynard@krebs.com\n\n"
"Hyperlinks\n"
"<a href=\"http://www.codeproject.com\">The Code Project</a>\n"
"Href=\"http://ultrapico.com/Expresso.htm\"\n\n"
"Key-Value Pairs\n"
"FurryAnimal=Dog\n"
"NonFurryAnimal=Turtle\n\n"
"HTML Tags\n"
"<p>Hello World</p>\n"
"<H1>Regular Expressions</H1>\n\n"
"Social Security Numbers\n"
"123-45-6789\n"
"987-65-4321\n\n"
"Mastercard\n"
"5134567890123456\n"
"Visa\n"
"4234567890123\n"
"Amex\n"
"343456789012345\n"
"Diners Club\n"
"30045678901234\n"
"Discover\n"
"6011567890123456\n\n"
"Numbers\n"
"123.56\n"
"-234.5\n"
"12\n"
"+13\n"
"0.56\n"
"-.2\n"
".3\n"
"25.\n"
"1.38E-23\n"
"0.2E45\n"
"6E+19\n"
"0AFD\n"
"1CA0234F")
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