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

r"
"
gm

Test String

Code Generator

Generated Code

re = /(([a-hKQNB]x)?(R[1-8]?[a-f]?x)?[KQNBR]?[abcdefgh]([1-8]{1}(?!=))(\(ep\))?((?<=[18])=[KQNBR])?\+{,2}|0(-0){1,2})/m str = ' <dt>e4</dt><dd>match</dd> <dt>a8</dt><dd>match</dd> <dt>g5</dt><dd>match</dd> <dt>h7</dt><dd>match</dd> <dt>a1</dt><dd>match</dd> <dt>Kd2</dt><dd>match</dd> <dt>Kg6</dt><dd>match</dd> <dt>Qh4</dt><dd>match</dd> <dt>Qe2</dt><dd>match</dd> <dt>Qb7</dt><dd>match</dd> <dt>Nf2</dt><dd>match</dd> <dt>Nc6</dt><dd>match</dd> <dt>Be4</dt><dd>match</dd> <dt>Bf3</dt><dd>match</dd> <dt>Bb2</dt><dd>match</dd> <dt>Ra1</dt><dd>match</dd> <dt>Rc3</dt><dd>match</dd> <dt>Rh8</dt><dd>match</dd> <dt>0-0</dt><dd>match</dd> <dt>0-0-0</dt><dd>match</dd> <dt>exd5</dt><dd>match</dd> <dt>axb7</dt><dd>match</dd> <dt>exd2</dt><dd>match</dd> <dt>Bxc6</dt><dd>match</dd> <dt>Bxa4</dt><dd>match</dd> <dt>Qxe2</dt><dd>match</dd> <dt>Qxb7</dt><dd>match</dd> <dt>Nxf2</dt><dd>match</dd> <dt>Nxb2</dt><dd>match</dd> <dt>Bxb2</dt><dd>match</dd> <dt>Rxh8</dt><dd>match</dd> <dt>Rxc3</dt><dd>match</dd> <dt>exd5(ep)</dt><dd>match</dd> <dt>exd2(ep)</dt><dd>match</dd> <dt>a1=Q</dt><dd>match</dd> <dt>d1=Q</dt><dd>match</dd> <dt>f8=Q</dt><dd>match</dd> <dt>e8=Q</dt><dd>match</dd> <dt>a1=N</dt><dd>match</dd> <dt>b8=B</dt><dd>match</dd> <dt>f1=R</dt><dd>match</dd> <dt>d8=Q</dt><dd>match</dd> <dt>bxc1=Q</dt><dd>match</dd> <dt>exd1=Q</dt><dd>match</dd> <dt>Raxc1</dt><dd>match</dd> <dt>Rfxd2</dt><dd>match</dd> <dt>R2xc1</dt><dd>match</dd> <dt>R5xd2</dt><dd>match</dd> <dt>Bxc6+</dt><dd>match</dd> <dt>Rxh8+</dt><dd>match</dd> <dt>Rh8+</dt><dd>match</dd> <dt>Bxb2+</dt><dd>match</dd> <dt>Rxc3++</dt><dd>match</dd> <dt>Bxc6++</dt><dd>match</dd> <dt>d8=Q++</dt><dd>match</dd> <dt>2</dt><dd>no match</dd> <dt>b</dt><dd>no match</dd> <dt>i2</dt><dd>no match</dd> <dt>a9</dt><dd>no match</dd> <dt>5d</dt><dd>no match</dd> <dt>K2</dt><dd>no match</dd> <dt>Ki6</dt><dd>no match</dd> <dt>Kf9</dt><dd>no match</dd> <dt>Qp3</dt><dd>no match</dd> <dt>Qd9</dt><dd>no match</dd> <dt>Q3</dt><dd>no match</dd> <dt>N</dt><dd>no match</dd> <dt>Nz9</dt><dd>no match</dd> <dt>Na10</dt><dd>no match</dd> <dt>B2</dt><dd>no match (watch case)</dd> <dt>Bb</dt><dd>no match</dd> <dt>Bi4</dt><dd>no match</dd> <dt>Ri5</dt><dd>no match</dd> <dt>Ra9</dt><dd>no match</dd> <dt>Sa2</dt><dd>no match</dd> <dt>Zf3</dt><dd>no match</dd> <dt>Jj2</dt><dd>no match</dd> <dt>1-1</dt><dd>no match</dd> <dt>0</dt><dd>no match</dd> <dt>2-1</dt><dd>no match</dd> <dt>0-1</dt><dd>no match</dd> <dt>0-0-1</dt><dd>no match</dd> <dt>0-0-0-0</dt><dd>no match</dd> <dt>0--0</dt><dd>no match</dd> <dt>bxa9</dt><dd>no match</dd> <dt>hxi2</dt><dd>no match</dd> <dt>Kxf9</dt><dd>no match</dd> <dt>Qxp3</dt><dd>no match</dd> <dt>Nxz9</dt><dd>no match</dd> <dt>Bxi4</dt><dd>no match</dd> <dt>Rxi5</dt><dd>no match</dd> <dt>a1=</dt><dd>no match</dd> <dt>a2=Q</dt><dd>no match</dd> <dt>b4=N</dt><dd>no match</dd> <dt>d1=S</dt><dd>no match</dd> <dt>d5=G</dt><dd>no match</dd> <dt>exd5=G</dt><dd>no match</dd> <dt>exd1=S</dt><dd>no match</dd> <dt>bxa1=</dt><dd>no match</dd> <dt>Rdxd2</dt><dd>no match</dd> <dt>Rexe2</dt><dd>no match</dd> <dt>R2xe2</dt><dd>no match</dd> <dt>Bxi4++</dt><dd>no match</dd>' # Print the match result str.scan(re) do |match| puts match.to_s end

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 Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html