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 = '/^([^\s]+)(\s\d?)(.*)$/m'; $str = 'CH4 ENGLAND CH4 GB CH4 LONDON CH4 LONDON/SOUTH CH4 LONDON/SOUTH/MIDLANDS CH4 MIDLANDS CH4 NETWORK CH4 NORTH CH4 SCOTLAND CH4 SOUTH CH4 ULSTER CH4+1 ENGLAND CH4+1 GB CH4+1 LONDON CH4+1 LONDON/SOUTH CH4+1 LONDON/SOUTH/MIDLANDS CH4+1 MIDLANDS CH4+1 NETWORK CH4+1 NORTH CH4+1 SCOTLAND CH4+1 SOUTH CH4+1 ULSTER CHANNEL 5 NETWORK CHANNEL 5+1 NETWORK ITV ANGLIA ITV BORDER ITV BREAKFAST ITV BREAKFAST CENTRAL ITV BREAKFAST ENG & WALES ITV BREAKFAST GREAT BRITAIN ITV BREAKFAST HD - ULSTER ITV BREAKFAST HD CENTRAL ITV BREAKFAST HD ENG & WALES ITV BREAKFAST HD GB ITV BREAKFAST HD LITTLEBRITAIN ITV BREAKFAST HD LONDON ITV BREAKFAST HD NETWORK ITV BREAKFAST HD NORTH ITV BREAKFAST HD SCOTLAND ITV BREAKFAST HD SOUTH ITV BREAKFAST LITTLEBRITAIN ITV BREAKFAST LONDON ITV BREAKFAST NORTH ITV BREAKFAST SCOTLAND ITV BREAKFAST SOUTH ITV BREAKFAST ULSTER ITV BREAKFAST+1 - ENG & WAL ITV BREAKFAST+1 - GB ITV BREAKFAST+1 - LONDON ITV BREAKFAST+1 - MIDLANDS ITV BREAKFAST+1 - NETWORK ITV BREAKFAST+1 - NORTH ITV BREAKFAST+1 - NTH/STH/MID ITV BREAKFAST+1 - SCOTLAND ITV BREAKFAST+1 - SOUTH ITV BREAKFAST+1 - ULSTER ITV CENTRAL ITV GRANADA ITV GRANADA BORDER MACRO ITV HD ANGLIA ITV HD BORDER ITV HD CENTRAL ITV HD GRANADA ITV HD GRANADA BORDER MACRO ITV HD LONDON CARLTON ITV HD LONDON LWT ITV HD MERIDIAN ITV HD MIDWEST MACRO ITV HD NORTH MACRO ITV HD SOUTH EAST MACRO ITV HD STV MACRO ITV HD TYNE TEES ITV HD UTV ITV HD WALES ITV HD WEST ITV HD WESTCOUNTRY ITV HD YORKSHIRE ITV HD YORKSHIRE/TT MACRO ITV LONDON CARLTON ITV LONDON LWT ITV MERIDIAN ITV MIDWEST MACRO ITV NORTH MACRO ITV SOUTH EAST MACRO ITV STV MACRO ITV TYNE TEES ITV UTV ITV WALES ITV WALES & WEST ITV WEST ITV WEST MACRO ITV WESTCOUNTRY ITV YORKSHIRE ITV YORKSHIRE TYNE TEES MACRO ITV+1- ANGLIA ITV+1 - CARLTON ITV+1 - CENTRAL ITV+1 - GRANADA/BORDER MACRO ITV+1 - LWT ITV+1 - MERIDIAN ITV+1 - MIDWEST MACRO ITV+1 - NORTH MACRO ITV+1 - SCOTLAND MACRO ITV+1 - SOUTH EAST MACRO ITV+1 - TYNE TEES ITV+1 - ULSTER ITV+1 - WALES ITV+1 - WALES + WEST MACRO ITV+1 - WEST ITV+1 - WEST MACRO ITV+1 - WESTCOUNTRY ITV+1 - YORKSHIRE ITV+1 - YORKSHIRE/TT MACRO S4C UK '; 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