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

Code Generator

Generated Code

$re = '/^(?\'username\'.*):.*:(?\'UID\'.*):(?\'GID\'.*):(?\'GECOS\'.*):(?\'home\'.*):(?\'shell\'.*)$/m'; $str = 'root:x:0:0:Root User:/root:/bin/bash john:x:1001:1001:John Smith,Room 101,+11234567890,,johnsmith@email.com:/home/john:/bin/bash jane:x:1002:1002:Jane Doe,Room 102,+442012345678,,jane.doe@email.co.uk:/home/jane:/bin/bash mike:x:1003:1003:Michael Johnson,Room 201,+498912345678,,michael.johnson@email.de:/home/mike:/bin/bash sarah:x:1004:1004:Sarah Brown,Room 202,+61255512345,,sarah.brown@email.au:/home/sarah:/bin/bash karen:x:1005:1005:Karen Miller,Room 203,+12155551234,,karen.miller@email.com:/home/karen:/bin/bash jason:x:1006:1006:Jason Lee,Room 204,+81312345678,,jason.lee@email.jp:/home/jason:/bin/bash steve:x:1007:1007:Steven Wilson,Room 205,+33123456789,,steven.wilson@email.fr:/home/steve:/bin/bash emma:x:1008:1008:Emma Johnson,Room 206,+13455551234,,emma.johnson@email.com:/home/emma:/bin/bash olivia:x:1009:1009:Olivia Brown,Room 207,+441234567890,,olivia.brown@email.co.uk:/home/olivia:/bin/bash lucas:x:1010:1010:Lucas White,Room 208,+61455512345,,lucas.white@email.au:/home/lucas:/bin/bash isabella:x:1011:1011:Isabella Adams,Room 209,+41123456789,,isabella.adams@email.ch:/home/isabella:/bin/bash liam:x:1012:1012:Liam Thompson,Room 210,+17345551234,,liam.thompson@email.com:/home/liam:/bin/bash noah:x:1013:1013:Noah Scott,Room 211,+17345551235,,noah.scott@email.com:/home/noah:/bin/bash ava:x:1014:1014:Ava Clark,Room 212,+61255512346,,ava.clark@email.au:/home/ava:/bin/bash sophia:x:1015:1015:Sophia Turner,Room 213,+33123456790,,sophia.turner@email.fr:/home/sophia:/bin/bash'; preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0); // Print the entire match result var_dump($matches);

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 PHP, please visit: http://php.net/manual/en/ref.pcre.php