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
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
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • 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

/
/
gmx

Test String

Code Generator

Generated Code

re = /^ (?<born> # BORN part start (?:(?<o>1)|(?<t>2)|(?<h>3)|(?<f>4)|(?<v>5)|(?<s>6)|(?<n>7)|(?<e>8)|(?<i>9)|(?<z>0)) # Thousands (?:(?<o2>1)|(?<t2>2)|(?<h2>3)|(?<f2>4)|(?<v2>5)|(?<s2>6)|(?<n2>7)|(?<e2>8)|(?<i2>9)|(?<z2>0)) # Centuries (?:(?<o3>1)|(?<t3>2)|(?<h3>3)|(?<f3>4)|(?<v3>5)|(?<s3>6)|(?<n3>7)|(?<e3>8)|(?<i3>9)|(?<z3>0)) # Decades (?:(?<o4>1)|(?<t4>2)|(?<h4>3)|(?<f4>4)|(?<v4>5)|(?<s4>6)|(?<n4>7)|(?<e4>8)|(?<i4>9)|(?<z4>0)) # Last digit ) , (?!\k<born>) #Fail if years are identical (?: #1st digit is lower (?:(?(o)0|(?(t)[01]|(?(h)[0-2]|(?(f)[0-3]|(?(v)[0-4]|(?(s)[0-5]|(?(n)[0-6]|(?(e)[0-7]|(?(i)[0-8]|-1)))))))))) \d{3} | #1st digit is lower or identical (?:(?(o)[01]|(?(t)[0-2]|(?(h)[0-3]|(?(f)[0-4]|(?(v)[0-5]|(?(s)[0-6]|(?(n)[0-7]|(?(e)[0-8]|(?(i)[0-9]|-1)))))))))) #and 2nd is lower (?:(?(o2)0|(?(t2)[01]|(?(h2)[0-2]|(?(f2)[0-3]|(?(v2)[0-4]|(?(s2)[0-5]|(?(n2)[0-6]|(?(e2)[0-7]|(?(i2)[0-8]|-1)))))))))) \d{2} | #1st digit is lower or identical (?:(?(o)[01]|(?(t)[0-2]|(?(h)[0-3]|(?(f)[0-4]|(?(v)[0-5]|(?(s)[0-6]|(?(n)[0-7]|(?(e)[0-8]|(?(i)[0-9]|-1)))))))))) #and 2nd is lower or identical (?:(?(o2)[01]|(?(t2)[0-2]|(?(h2)[0-3]|(?(f2)[0-4]|(?(v2)[0-5]|(?(s2)[0-6]|(?(n2)[0-7]|(?(e2)[0-8]|(?(i2)[0-9]|-1)))))))))) #and 3rd is lower (?:(?(o3)0|(?(t3)[01]|(?(h3)[0-2]|(?(f3)[0-3]|(?(v3)[0-4]|(?(s3)[0-5]|(?(n3)[0-6]|(?(e3)[0-7]|(?(i3)[0-8]|-1)))))))))) \d | #1st digit is lower or identical (?:(?(o)[01]|(?(t)[0-2]|(?(h)[0-3]|(?(f)[0-4]|(?(v)[0-5]|(?(s)[0-6]|(?(n)[0-7]|(?(e)[0-8]|(?(i)[0-9]|-1)))))))))) #and 2nd is lower or identical (?:(?(o2)[01]|(?(t2)[0-2]|(?(h2)[0-3]|(?(f2)[0-4]|(?(v2)[0-5]|(?(s2)[0-6]|(?(n2)[0-7]|(?(e2)[0-8]|(?(i2)[0-9]|-1)))))))))) #and 3rd is lower or identical (?:(?(o3)[01]|(?(t3)[0-2]|(?(h3)[0-3]|(?(f3)[0-4]|(?(v3)[0-5]|(?(s3)[0-6]|(?(n3)[0-7]|(?(e3)[0-8]|(?(i3)[0-9]|-1)))))))))) #and 4th is lower (?:(?(o4)0|(?(t4)[01]|(?(h4)[0-2]|(?(f4)[0-3]|(?(v4)[0-4]|(?(s4)[0-5]|(?(n4)[0-6]|(?(e4)[0-7]|(?(i4)[0-8]|-1)))))))))) ) $/mx str = '1852,1891 1862,1862 1945,1944 1933,1922 1902,1785 1923,0923 0001,0999 0001,0990 0001,0989 0001,0988 0001,0987 0001,0986' # 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