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

use strict; my $str = '-- TEST 2 WITH COLUMNS CREATE( (LOCAL )?(?P<temporal>TEMPORARY|TEMP))? TABLE( IF NOT EXISTS)? (?P<schema_name>[{ }\\w_]+).(?P<table_name>[{ }\\w_]+) \\( (?P<columns>([\\w _](\\(\\d+\\))?,?)+) \\) -- COLUMNS \\( ([\\w _](\\(\\d+\\))?,?)+ \\) -- COLUMNA (?P<column>([\\w _](\\(\\d+\\))?)+) -- TABLE ATTRIBUTE (DISTSTYLE (?P<dist_style>(EVEN|KEY|ALL)))|(DISTKEY\\((?P<dist_key>[\\w_]+)\\))|((?P<sort_key_type>(COMPOUND|INTERLEAVED) )?SORTKEY \\((?P<sort_keys>([\\w _,]+))\\)) 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) DROP TABLE IF EXISTS {{ dm_site_schema }}.ad_cohort; --------------------------------------- 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) --------------------------------------- DROP TABLE IF EXISTS {{staging_site_schema}}.state_params --------------------------------------- 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) --------------------------------------- DROP TABLE IF EXISTS {{staging_site_schema}}.users --------------------------------------- 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) --------------------------------------- --------------------------------------- CREATE TABLE {{ staging_site_schema }}.ad_params ( ad_id INTEGER NOT NULL ENCODE DELTA32k, name VARCHAR(32), value VARCHAR(1024)) DISTKEY(ad_id) CREATE TABLE {{ staging_site_schema }}.ad_params ( ad_id INTEGER NOT NULL ENCODE DELTA32k, name VARCHAR(32), value VARCHAR(1024)) 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); CREATE [ [LOCAL ] { TEMPORARY | TEMP } ] TABLE [ IF NOT EXISTS ] table_name ( { column_name data_type [column_attributes] [ column_constraints ] | table_constraints | LIKE parent_table [ { INCLUDING | EXCLUDING } DEFAULTS ] } [, ... ] ) [ BACKUP { YES | NO } ] [table_attribute] and table_attributes are: [ DISTSTYLE { EVEN | KEY | ALL } ] [ DISTKEY ( column_name ) ] [ [COMPOUND | INTERLEAVED ] SORTKEY ( column_name [, ...] ) ]'; my $regex = qr/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 _,]+))\))?/mip; if ( $str =~ /$regex/g ) { print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n"; # print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n"; # print "Capture Group 2 is $2 ... and so on\n"; } # ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p' # Named capture groups can be called via $+{name}

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 Perl, please visit: http://perldoc.perl.org/perlre.html