# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"matei 170 associative arrays"
test_str = ("<!DOCTYPE html>\n"
"<html lang=\"en\">\n"
"<head>\n"
" <meta charset=\"UTF-8\">\n"
" <title>Community Login</title>\n"
"</head>\n"
"<body>\n\n"
"<h2>Community Login</h2>\n\n"
"<form method=\"post\">\n"
" <label for=\"username\">Username:</label>\n"
" <input type=\"text\" name=\"username\" id=\"username\"><br><br>\n\n"
" <label for=\"password\">Password:</label>\n"
" <input type=\"password\" name=\"password\" id=\"password\"><br><br>\n\n"
" <button type=\"submit\" name=\"login\">Login</button>\n"
"</form>\n\n"
"<?php\n"
"$communityUsers = [\n"
" \"GAM\" => password_hash(\"gamgamstyle\", PASSWORD_ARGON2ID),\n"
" \"MAO\" => password_hash(\"gentoo\", PASSWORD_ARGON2ID),\n"
" \"MaxiObe\" => password_hash(\"iloveallmias\", PASSWORD_ARGON2ID)\n"
"];\n\n"
"if (isset($_POST['login'])) {\n"
" $enteredUsername = $_POST['username'];\n"
" $enteredPassword = $_POST['password'];\n\n"
" if (!empty($enteredUsername) && !empty($enteredPassword)) {\n"
" if (array_key_exists($enteredUsername, $communityUsers)) {\n"
" if (password_verify($enteredPassword, $communityUsers[$enteredUsername])) {\n"
" echo \"<p style='color:green;'>Login successful! Welcome, \" . htmlspecialchars($enteredUsername) . \" </p>\";\n"
" } else {\n"
" echo \"<p style='color:red;'>Incorrect password.</p>\";\n"
" }\n"
" } else {\n"
" echo \"<p style='color:red;'>Username not found.</p>\";\n"
" }\n"
" } else {\n"
" echo \"<p style='color:red;'>Please fill in both fields!</p>\";\n"
" }\n"
"}\n"
"?>\n\n"
"</body>\n"
"</html>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\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