Regular Expressions 101

Save & Share

  • Regex Version: ver. 1
  • Update Regex
    ctrl+⇧+s
  • Save new Regex
    ctrl+s
  • Add to Community Library

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8
  • .NET 7.0 (C#)
  • Rust
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests

Tools

Sponsors
There are currently no sponsors. Become a sponsor today!
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression

/
/
gm

Test String

Code Generator

Generated Code

# 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