# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^(?<vendor_date>\d{4}(?:\-\d\d){2} [^,]+,[^:]+):(?<vendor_level>[^:]+):(?<vendor_component>[^:]+):(?<vendor_message>[^\n\r]+)(?<vendor_details>(?(?=^\d{4}(?:\-\d\d){2})|[^\r])+)?"
test_str = ("2019-10-14 11:07:08,727:DEBUG:certbot.cli:Var dry_run=True (set by user).\n"
"2019-10-14 11:07:08,727:DEBUG:certbot.cli:Var server={'dry_run', 'staging'} (set by user).\n"
"2019-10-14 11:07:08,727:DEBUG:certbot.cli:Var dry_run=True (set by user).\n"
"2019-10-14 11:07:08,728:DEBUG:certbot.cli:Var server={'dry_run', 'staging'} (set by user).\n"
"2019-10-14 11:07:08,728:DEBUG:certbot.cli:Var account={'server'} (set by user).\n"
"2019-10-14 11:07:08,757:INFO:certbot.renewal:Cert not due for renewal, but simulating renewal for dry run\n"
"2019-10-14 11:07:08,757:DEBUG:certbot.plugins.selection:Requested authenticator nginx and installer nginx\n"
"2019-10-14 11:07:10,881:DEBUG:certbot.plugins.selection:Single candidate plugin: * nginx\n"
"Description: Nginx Web Server plugin\n"
"Interfaces: IAuthenticator, IInstaller, IPlugin\n"
"Entry point: nginx = certbot_nginx.configurator:NginxConfigurator\n"
"Initialized: \n"
"Prep: True\n"
"2019-10-14 11:07:10,888:DEBUG:certbot.plugins.selection:Single candidate plugin: * nginx\n"
"Description: Nginx Web Server plugin\n"
"Interfaces: IAuthenticator, IInstaller, IPlugin\n"
"Entry point: nginx = certbot_nginx.configurator:NginxConfigurator\n"
"Initialized: \n"
"Prep: True\n"
"2019-10-14 11:07:10,889:DEBUG:certbot.plugins.selection:Selected authenticator and installer \n"
"2019-10-14 11:07:10,889:INFO:certbot.plugins.selection:Plugins selected: Authenticator nginx, Installer nginx\n"
"2019-10-14 11:07:10,902:DEBUG:certbot.main:Picked account: )>), only_return_existing=None, contact=('mailto:trol@yopmail.com',), agreement='https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf')), 019763ae9d0030f2ffa8f8cb689e7976, Meta(creation_dt=datetime.datetime(2018, 2, 27, 14, 17, 55, tzinfo=), creation_host='sd-99715.dedibox.fr'))>\n"
"2019-10-14 11:07:10,911:DEBUG:acme.client:Sending GET request to https://acme-staging-v02.api.letsencrypt.org/directory.\n"
"2019-10-14 11:07:10,998:DEBUG:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): acme-staging-v02.api.letsencrypt.org\n"
"2019-10-14 11:07:14,617:DEBUG:requests.packages.urllib3.connectionpool:https://acme-staging-v02.api.letsencrypt.org:443 \"GET /directory HTTP/1.1\" 200 724\n"
"2019-10-14 11:07:14,619:DEBUG:acme.client:Received response:\n"
"HTTP 200\n"
"Server: nginx\n"
"Date: Mon, 14 Oct 2019 09:07:14 GMT\n"
"Content-Type: application/json\n"
"Content-Length: 724\n"
"Connection: keep-alive\n"
"Cache-Control: public, max-age=0, no-cache\n"
"X-Frame-Options: DENY\n"
"Strict-Transport-Security: max-age=604800\n"
"{\n"
" \"kRr68eKT_x4\": \"https://community.letsencrypt.org/t/adding-random-entries-to-the-directory/33417\",\n"
" \"keyChange\": \"https://acme-staging-v02.api.letsencrypt.org/acme/key-change\",\n"
" \"meta\": {\n"
" \"caaIdentities\": [\n"
" \"letsencrypt.org\"\n"
" ],\n"
" \"termsOfService\": \"https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf\",\n"
" \"website\": \"https://letsencrypt.org/docs/staging-environment/\"\n"
" },\n"
" \"newAccount\": \"https://acme-staging-v02.api.letsencrypt.org/acme/new-acct\",\n"
" \"newNonce\": \"https://acme-staging-v02.api.letsencrypt.org/acme/new-nonce\",\n"
" \"newOrder\": \"https://acme-staging-v02.api.letsencrypt.org/acme/new-order\",\n"
" \"revokeCert\": \"https://acme-staging-v02.api.letsencrypt.org/acme/revoke-cert\"\n"
"}\n"
"2019-10-14 11:07:14,620:INFO:certbot.main:Renewing an existing certificate\n"
"2019-10-14 11:07:14,999:DEBUG:acme.client:Requesting fresh nonce\n"
"2019-10-14 11:07:15,006:DEBUG:acme.client:Sending HEAD request to https://acme-staging-v02.api.letsencrypt.org/acme/new-nonce.\n"
"2019-10-14 11:07:15,156:DEBUG:requests.packages.urllib3.connectionpool:https://acme-staging-v02.api.letsencrypt.org:443 \"HEAD /acme/new-nonce HTTP/1.1\" 200 0\n"
"2019-10-14 11:07:15,158:DEBUG:acme.client:Received response:\n"
"HTTP 200\n"
"Server: nginx\n"
"Date: Mon, 14 Oct 2019 09:07:15 GMT\n"
"Connection: keep-alive\n"
"Cache-Control: public, max-age=0, no-cache\n"
"Link: https://acme-staging-v02.api.letsencrypt.org/directory;rel=\"index\"\n"
"Replay-Nonce: 0002fqXrdxZwV_xxxxx-xxxxxxxx-KdWhvOCtgFh5YNNVM\n"
"X-Frame-Options: DENY\n"
"Strict-Transport-Security: max-age=604800\n"
"2019-10-14 11:07:15,158:DEBUG:acme.client:Storing nonce: 0002fqXrdxZwV_xxxxx-xxxxxxxx-KdWhvOCtgFh5YNNVM \n"
"2019-10-14 11:07:15,160:DEBUG:acme.client:JWS payload:\n"
"b'{\\n \"identifiers\": [\\n {\\n \"type\": \"dns\",\\n \"value\": \"site.tld\"\\n },\\n {\\n \"type\": \"dns\",\\n \"value\": \"cdn-css. site.tld \"\\n },\\n {\\n \"type\": \"dns\",\\n \"value\": \"cdn-js-body. site.tld \"\\n },\\n {\\n \"type\": \"dns\",\\n \"value\": \"cdn-js-footer. site.tld \"\\n },\\n {\\n \"type\": \"dns\",\\n \"value\": \"cdn-js-head. site.tld \"\\n },\\n {\\n \"type\": \"dns\",\\n \"value\": \"www. site.tld \"\\n }\\n ]\\n}'\n"
"2019-10-14 11:07:15,173:DEBUG:acme.client:Sending POST request to https://acme-staging-v02.api.letsencrypt.org/acme/new-order:\n"
"{\n"
" \"protected\": \"SOmeBASE64Str\",\n"
" \"payload\": \"ANOTHERB64STR\",\n"
" \"signature\": \"YETANOTHERB64STR\"\n"
"}\n"
"2019-10-14 11:07:15,387:DEBUG:requests.packages.urllib3.connectionpool:https://acme-staging-v02.api.letsencrypt.org:443 \"POST /acme/new-order HTTP/1.1\" 201 1104")
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