Regular Expressions 101

Save & Share

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

Substitution

Processing...

Code Generator

Generated Code

const regex = /((^0)\s+|(^1)\s+|(^2)\s+|(^3)\s+|(^4)\s+|(^5)\s+)((((.{4})(.+)))|((.{4})))/gm; // Alternative syntax using RegExp constructor // const regex = new RegExp('((^0)\\s+|(^1)\\s+|(^2)\\s+|(^3)\\s+|(^4)\\s+|(^5)\\s+)((((.{4})(.+)))|((.{4})))', 'gm') const str = `0 @I0066@ INDI 1 NAME Joan Adrienne /Hatch/ 2 GIVN Joan Adrienne 2 SURN Hatch 2 SOUR @S0045@ 3 DATA 4 TEXT Birth date: 30 May 1935 Birth place: Hennepin, Minnesota 2 SOUR @S0131@ 3 PAGE Source Citation: Year: 1940; Census Place: Faribault, Rice, Minnesota; Roll: T627_1953; Page: 8B; Enumeration District: 66-9A 3 DATA 4 TEXT residence date: 1 Apr 1940 residence place: Faribault, Rice, Minnesot 5 CONC a, United States birth date: May 30, 1935 birth place: Minneapolis 5 CONC , Hennepin, Minnesota, USA Name: Joan Adrienne Hatch 3 NOTE @N0121@ 2 SOUR @S0635@ 3 DATA 4 TEXT Marriage date: 29 Dec 1953 Marriage place: Cook County, IL 1 SEX F 1 BIRT 2 DATE 30 MAY 1935 2 PLAC Minneapolis, Hennepin, Minnesota, USA 2 SOUR @S0045@ 3 DATA 4 TEXT Birth date: 30 May 1935 Birth place: Hennepin, Minnesota 2 SOUR @S0131@ 3 PAGE Source Citation: Year: 1940; Census Place: Faribault, Rice, Minnesota; Roll: T627_1953; Page: 8B; Enumeration District: 66-9A 3 DATA 4 TEXT residence date: 1 Apr 1940 residence place: Faribault, Rice, Minnesot 5 CONC a, United States birth date: May 30, 1935 birth place: Minneapolis 5 CONC , Hennepin, Minnesota, USA Name: Joan Adrienne Hatch 3 NOTE @N0125@ 1 DEAT 2 DATE 14 DEC 1989 2 PLAC Nyack, Rockland, New York, USA 1 RESI 2 DATE 1 APR 1940 2 PLAC Faribault, Rice, Minnesota, USA 2 SOUR @S0131@ 3 PAGE Source Citation: Year: 1940; Census Place: Faribault, Rice, Minnesota; Roll: T627_1953; Page: 8B; Enumeration District: 66-9A 3 DATA 4 TEXT residence date: 1 Apr 1940 residence place: Faribault, Rice, Minnesot 5 CONC a, United States birth date: May 30, 1935 birth place: Minneapolis 5 CONC , Hennepin, Minnesota, USA Name: Joan Adrienne Hatch 3 NOTE @N0127@ 1 RESI 2 DATE 1 APR 1940 2 PLAC Faribault, Rice, Minnesota, USA 2 SOUR @S0131@ 3 PAGE Source Citation: Year: 1940; Census Place: Faribault, Rice, Minnesota; Roll: T627_1953; Page: 8B; Enumeration District: 66-9A 3 DATA 4 TEXT residence date: 1 Apr 1940 residence place: Faribault, Rice, Minnesot 5 CONC a, United States birth date: May 30, 1935 birth place: Minneapolis 5 CONC , Hennepin, Minnesota, USA Name: Joan Adrienne Hatch 3 NOTE @N0129@ 1 FAMC @F1196@ 1 FAMS @F0129@ 1 FAMS @F0260@ 1 CHAN 2 DATE 31 MAR 2017 3 TIME 22:03:40`; const subst = `\n $1 | $2 | $3 | $4 | $5 | $6 | $7 | $8 | $9 | $10 | $11 | $12 | $13 | $14 | |`; // The substituted value will be contained in the result variable const result = str.replace(regex, subst); console.log('Substitution result: ', result);

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 JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions