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
No Match

/
/
gm

Test String

Code Generator

Generated Code

const regex = /((?<text>\w+\sS-bane den\s*)(?<date>\d{1,2}\.\s\w+\s\d{4}))/gm; // Alternative syntax using RegExp constructor // const regex = new RegExp('((?<text>\\w+\\sS-bane den\\s*)(?<date>\\d{1,2}\\.\\s\\w+\\s\\d{4}))', 'gm') const str = `Toganmeldelse nummer 50034 Side 1 af 2 Banedanmark Trafikstyring S-bane den 5. marts 2019 11.03 – 12.03 Dyt – Sam højre spor Linje B MSGD Modtagere Banedanmark DSB Andre Driftscenter Hovedstad Høje Taastrup kommandopost* S-tog Drift* Driftsplanlægning Bestiller: Se nedenfor Kvittering: Modtagere der er markeret med en *, skal kvittere til e-mail: dch@bane.dk Bestiller Driftsplanlægning, DSB S-tog, dp@s-tog.dsb.dk Bestiller sørger for at dette cirkulære fordeles til relevante enheder, der ikke er nævnt som modtager Toganmeldelse nummer 50034 (S-banen) Delaflysninger Tognumre Strækning Ingen Aflysninger Tognumre Strækning 22263 – 22201 22602 – 22603 Farum – Høje Taastrup Farum – København H 11.03 12.03 Tillysninger Tognumre Strækning 23059 26263 – 26201 26602 – 26603 Farum – København H Farum – Høje Taastrup Farum – København H 11.03 12.03 23059 Farum Hareskov Buddinge Dyssegård Svanemøllen Østerport København H 19.12.00 (19.19.06) (19.27.48) (19.33.06) (19.40.30) (19.45.06 19.52.00 Befordrer Materiel Toganmeldelse nummer 50034 Side 2 af 2 Linje B Farum – Høje Taastrup Farum Minut Minut Minut Op- hold i sek. Minuttal afg Afg 02.00 22.00 42.00 (pass) Time Tog nr Tog nr Tog nr Farum - 02.00 22.00 42.00 0 26602 1 26603 1 - Værløse 15 06.00 26.00 46.00 1 - - - Hareskov 10 09.06 29.06 49.06 2 - - - Skovbrynet 10 11.30 31.30 51.30 3 - - - Bagsværd 20 13.36 33.36 53.36 4 - - - Stengården 10 15.24 35.24 55.24 5 - - - Buddinge 20 17.48 37.48 57.48 6 - - - Kildebakke 10 19.30 39.30 59.30 7 - - - Vangede 10 21.24 41.24 01.24 8 - - - Dyssegård 120 25.30 45.30 05.30 9 - - - Emdrup 15 28.00 48.00 08.00 10 - - - Ryparken 15 31.00 51.00 11.00 11 - - - Svanemøllen 20 34.30 54.30 14.30 12 - - - Nordhavn 20 36.42 56.42 16.42 13 - - - Østerport 25 39.06 59.06 19.06 14 - - - Nørreport 25 41.36 01.36 21.36 15 - - - Vesterport 20 43.30 03.30 23.30 16 - - - København H 60 46.00 06.00 26.00 17 - - - Dybbølsbro 20 47.54 07.54 27.54 18 - - - Skelbæk - (49.20) (09.20) (29.20) 19 - - - Carlsberg 20 50.30 10.30 30.30 20 - 26263 26264 Valby 25 52.48 12.48 32.48 21 26265 26266 26267 Danshøj 15 54.42 14.42 34.42 22 26268 26269 26270 Hvidovre 20 56.24 16.24 36.24 23 26271 26200 26201 Rødovre 20 58.06 18.06 38.06 Brøndbyøster 20 59.54 19.54 39.54 Glostrup 30 02.42 22.42 42.42 Albertslund 25 05.30 25.30 45.30 Taastrup 25 08.36 28.36 48.36 Høje Taastrup - 11.00 31.00 51.00 Befordrer Passagerer Bemærkning 1) Kører kun til København H `; // Reset `lastIndex` if this regex is defined globally // regex.lastIndex = 0; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); }

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