Regular Expressions 101

Save & Manage Regex

  • Current Version: 12
  • Save & Share
  • Community Library

Flavor

  • PCRE2 (PHP)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java
  • .NET 7.0 (C#)
  • Rust
  • PCRE (Legacy)
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests (18)
Sponsors
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

$re = '/(?:(?<=^\n)|(?<=\A)) (?<table> (?<table_caption>(?&t_caption))? # maybe some caption at the top? (?<table_headers> (?<table_header> # Table header (?<table_header_sep_top>(?&t_th_sep))? # Possible top separator line (?<table_header1> # First header row [^\n]+\n ) (?<table_header_sep>(?&t_th_sep)) # a separator (?<table_header2> # and possibly a second row of header [^\n]+\n (?<table_header_sep>(?&t_th_sep)) )? ) ) (?<table_rows> # Multiple table rows (?<table_row>(?&t_row))* ) (?<table_bottom_sep>(?&t_th_sep))? # Possibly ended by a separator line (?<table_caption>(?&t_caption))? # and maybe with some caption at the bottom (?=\n|\Z) ) (?(DEFINE) (?<t_th_sep> [\+\-\|](?:[\: ]*\-+[\: ]*[\+\-\|]?)+\n ) (?<t_row> [ ]{0,3} (?!(?&t_th_sep)|(?&t_caption)) (?: (?<tr_start_mark>[\|\:]+)? (?<tr_col_content>[^\|\:\n]+) (?: (?:[\|\:]{0,2}[ ]*(?=\n)) | [\|\:]{1,2} ) )+ (?: (?:\n(?=(?&t_th_sep))) | (?:\n(?=(?&t_caption))) | \n ) ) (?<t_caption> [ ]{0,3} \[[^\]]+\] [ ]* \n ) )/mxJ'; $str = '| | Grouping || +---------------+---------------------------------+ | First Header | Second Header | Third Header | +---------------+-----------------+---------------+ | Content | *Long Cell* || : continued : :: : content : :: | Content | **Cell** | Cell | : continued : : : : content : : : | New section | More | Data | | And more | And more || [Prototype table] Header 1 | Header 2 --------- | --------- Cell 1 | Cell 2 Cell 3 | Cell 4 With leading pipes: | Header 1 | Header 2 | --------- | --------- | Cell 1 | Cell 2 | Cell 3 | Cell 4 With tailing pipes: Header 1 | Header 2 | --------- | --------- | Cell 1 | Cell 2 | Cell 3 | Cell 4 | With leading and tailing pipes: | Header 1 | Header 2 | | --------- | --------- | | Cell 1 | Cell 2 | | Cell 3 | Cell 4 | * * * # One-column one-row table With leading pipes: | Header | ------- | Cell With tailing pipes: Header | ------- | Cell | With leading and tailing pipes: | Header | | ------- | | Cell | * * * Table alignement: | Default | Right | Center | Left | | --------- |:--------- |:---------:| ---------:| | Long Cell | Long Cell | Long Cell | Long Cell | | Cell | Cell | Cell | Cell | Table alignement (alternate spacing): | Default | Right | Center | Left | | --------- | :-------- | :-------: | --------: | | Long Cell | Long Cell | Long Cell | Long Cell | | Cell | Cell | Cell | Cell | * * * # Empty cells | Header 1 | Header 2 | | --------- | --------- | | A | B | | C | | Header 1 | Header 2 --------- | --------- A | B | D * * * # Missing tailing pipe Header 1 | Header 2 --------- | --------- | Cell | Cell | Cell | Cell | Header 1 | Header 2 | --------- | --------- Cell | Cell | Cell | Cell | Header 1 | Header 2 | --------- | --------- | Cell | Cell Cell | Cell | Header 1 | Header 2 | --------- | --------- | Cell | Cell | Cell | Cell * * * # Too many pipes in rows | Header 1 | Header 2 | | --------- | Cell | Cell | Extra cell? | | Cell | Cell | Extra cell? | '; 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