Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java
  • .NET 7.0 (C#)
  • Rust
  • PCRE (Legacy)
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests
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
Processing...

Test String

Code Generator

Generated Code

import re regex = re.compile(r"CREATE( (LOCAL )?(?P<temporal>TEMPORARY|TEMP))? TABLE( IF NOT EXISTS)? (?P<schema_name>[{ }\w_]+).(?P<table_name>[{ }\w_]+) ?\((?P<columns>([\w _](\(\d+\))?,?)+)\)( DISTSTYLE (?P<dist_style>(EVEN|KEY|ALL)))?( DISTKEY \((?P<dist_key>[\w_]+)\))?( (?P<sort_key_type>(COMPOUND|INTERLEAVED) )?SORTKEY \((?P<sort_keys>([\w _,]+))\))?", flags=re.MULTILINE | re.IGNORECASE) test_str = ("-- TEST 2 WITH COLUMNS\n" "CREATE( (LOCAL )?(?P<temporal>TEMPORARY|TEMP))? TABLE( IF NOT EXISTS)? (?P<schema_name>[{ }\\w_]+).(?P<table_name>[{ }\\w_]+) \\( (?P<columns>([\\w _](\\(\\d+\\))?,?)+) \\)\n\n" "-- COLUMNS\n" "\\( ([\\w _](\\(\\d+\\))?,?)+ \\)\n\n" "-- COLUMNA\n" "(?P<column>([\\w _](\\(\\d+\\))?)+)\n\n" "-- TABLE ATTRIBUTE\n" "(DISTSTYLE (?P<dist_style>(EVEN|KEY|ALL)))|(DISTKEY\\((?P<dist_key>[\\w_]+)\\))|((?P<sort_key_type>(COMPOUND|INTERLEAVED) )?SORTKEY \\((?P<sort_keys>([\\w _,]+))\\))\n\n\n\n" "CREATE TABLE {{schema}}.ad_params (er_id_site INTEGER ENCODE DELTA, er_id_ad VARCHAR(256) ENCODE LZO, ad_param_key VARCHAR(256), ad_param_value VARCHAR(256), id_ad_param_value VARCHAR(256), dt_audit_insert TIMESTAMP WITHOUT TIME ZONE ENCODE LZO, dt_audit_update TIMESTAMP WITHOUT TIME ZONE ENCODE LZO, dt_audit_analysis TIMESTAMP WITHOUT TIME ZONE ENCODE LZO) DISTKEY (er_id_ad) INTERLEAVED SORTKEY (er_id_site, er_id_ad, ad_param_key, id_ad_param_value)\n\n" "DROP TABLE IF EXISTS {{ dm_site_schema }}.ad_cohort;\n\n" "---------------------------------------\n" "CREATE TABLE {{staging_site_schema}}.ads (ad_id INTEGER NOT NULL ENCODE DELTA32k, list_id VARCHAR(32) ENCODE LZO, list_time VARCHAR(32) ENCODE LZO, status VARCHAR(32), name VARCHAR(128), region INTEGER, category INTEGER, user_id INTEGER, phone_hidden BOOL, no_salesmen BOOL, company_ad BOOL, subject VARCHAR(256) ENCODE LZO, body VARCHAR(65535) ENCODE LZO, price VARCHAR(16) ENCODE LZO, orig_list_time VARCHAR(32) ENCODE LZO, old_price VARCHAR(16) ENCODE LZO, modified_at VARCHAR(32) ENCODE LZO, lang VARCHAR(32), type VARCHAR(32), account_id INTEGER) DISTKEY(ad_id)\n" "---------------------------------------\n" "DROP TABLE IF EXISTS {{staging_site_schema}}.state_params\n" "---------------------------------------\n" "CREATE TABLE {{staging_site_schema}}.state_params (ad_id INTEGER encode raw, action_id INTEGER encode raw, state_id INT4 encode raw, name VARCHAR(20) encode raw, value VARCHAR(65535) encode raw) DISTKEY(ad_id)\n" "---------------------------------------\n" "DROP TABLE IF EXISTS {{staging_site_schema}}.users\n" "---------------------------------------\n" "CREATE TABLE {{staging_site_schema}}.users (user_id INTEGER ENCODE DELTA, uid INTEGER ENCODE DELTA, email VARCHAR(124) ENCODE LZO, account INTEGER ENCODE LZO, paid_total INTEGER ENCODE LZO) DISTKEY(user_id)\n" "---------------------------------------\n" "---------------------------------------\n" "CREATE TABLE {{ staging_site_schema }}.ad_params ( ad_id INTEGER NOT NULL ENCODE DELTA32k, name VARCHAR(32), value VARCHAR(1024)) DISTKEY(ad_id)\n\n" "CREATE TABLE {{ staging_site_schema }}.ad_params ( ad_id INTEGER NOT NULL ENCODE DELTA32k, name VARCHAR(32), value VARCHAR(1024)) \n" "CREATE TABLE {{ staging_site_schema }}.ad_params ( ad_id INTEGER NOT NULL ENCODE DELTA32k, name VARCHAR(32), value VARCHAR(1024)) DISTSTYLE ALL INTERLEAVED SORTKEY (dt_creation, dt_posting, er_id_region, er_id_site_location);\n\n\n\n" "CREATE [ [LOCAL ] { TEMPORARY | TEMP } ] TABLE \n" "[ IF NOT EXISTS ] table_name\n" "( { column_name data_type [column_attributes] [ column_constraints ] \n" " | table_constraints\n" " | LIKE parent_table [ { INCLUDING | EXCLUDING } DEFAULTS ] } \n" " [, ... ] )\n" "[ BACKUP { YES | NO } ]\n" "[table_attribute]\n\n" "and table_attributes are:\n" " [ DISTSTYLE { EVEN | KEY | ALL } ] \n" " [ DISTKEY ( column_name ) ]\n" " [ [COMPOUND | INTERLEAVED ] SORTKEY ( column_name [, ...] ) ]") matches = regex.finditer(test_str) for match_num, match in enumerate(matches, start=1): print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}") for group_num, group in enumerate(match.groups(), start=1): print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")

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