# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^(?<domain>[^/?#]+) wants you to sign in with your Ethereum account:\n(?<address>0x[a-zA-Z0-9]{40})\n\n((?<statement>[^\n]+)\n)?\nURI: (?<uri>(([^:?#]+):)?(([^?#]*))?([^?#]*)(\?([^#]*))?(#(.*))??)\nVersion: (?<version>1)\nChain ID: (?<chain_id>[0-9]+)\nNonce: (?<nonce>[a-zA-Z0-9]{8,})\nIssued At: (?<issued_at>([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([+|\-]([01][0-9]|2[0-3]):[0-5][0-9])))(\nExpiration Time: (?<expiration_time>([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))))?(\nNot Before: (?<not_before>([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))))?(\nRequest ID: (?<request_id>[-._~!$&'()*+,;=:@%a-zA-Z0-9]*))?(\nResources:(?<resources>(\n- (([^:?#]+):)?(([^?#]*))?([^?#]*)(\?([^#]*))?(#(.*))??)+))?$"
test_str = ("service.org wants you to sign in with your Ethereum account:\n"
"0xe5A12547fe4E872D192E3eCecb76F2Ce1aeA4946\n\n"
"I accept the ServiceOrg Terms of Service: https://service.org/tos\n\n"
"URI: https://service.org/login\n"
"Version: 1\n"
"Chain ID: 1\n"
"Nonce: 12341234\n"
"Issued At: 2022-03-17T12:45:13.610Z\n"
"Expiration Time: 2023-03-17T12:45:13.610Z\n"
"Not Before: 2022-03-17T12:45:13.610Z\n"
"Request ID: some_id\n"
"Resources:\n"
"- https://service.org/login")
matches = re.finditer(regex, test_str)
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