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

/
/

Test String

Code Generator

Generated Code

use strict; my $str = '//Good Data 123345T1234-5678KM 000001T1234-5678KM 001001T1234-5678KM 011001T1234-567KM 011011T1234-567KM 011111T1234-567KM 123345T123-5678KM 123345T123-567KM 123345T123-56KM 123345T12-56KM 123345T12-5KM 123345T1-5KM 123345T1234-567KM 123345T1234-567HF 123345T1234-567SM 123345T123-5678KM 123345T1234-567KKM 123345T123-567KKM 123345T123-567KHF 123345T123-567KSM 123345T123-56KKM 123345T12-56KKM 123345T12-5KKM 123345T12-5KKM 123345T1-5KKM 000359T1234-5678KM 123345T123K-5678KM 123345T12K-5678KM 123345T6K-5678KM 123345T123K-567KKM 123345T12K-56KKM 123345T6K-5KKM 123345T1234-5678M //Bad Data 1234567T1234-5678KM 12345T1234-5678KM 1245T1234-5678KM 145T1234-5678KM 14T1234-5678KM 1T1234-5678KM 123360T1234-5678KM 123345V1234-5678KM 123345T12345-5678KM 123345T1234-56789KM 123345T-5678KM 123345T123-KM 123345T123-56KMK 123345T1234-56KMK 123345T12-5678KKM 123345T1-5 123345T1234567KM 123345T1235678KKM 123345!56T123-567KKM 123!345T123-567KKM 123345T12!3-56KKM 123345T12-5!6KKM 123345T12-5K!KM 1233457T1234-5678K 123345T1234-5678GH 123345T1234-5678MK 123345T1234-5678UUUU null 000359T1234K-5678KM 123345TK-5678KM 123345T1234--5678KM '; my $regex = qr/([0-9]{3})([0-9]{3})(G|M|R|T{1})([0-9]{1,4}|[0-9]{1,3}K{0,1})(-)([0-9]{1,4}|[0-9]{1,3}K{0,1})(CM|FT|HM|HF|IN|KF|KM|MM|M|NM|SM|YD{1,2})/p; if ( $str =~ /$regex/ ) { print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n"; # print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n"; # print "Capture Group 2 is $2 ... and so on\n"; } # ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p' # Named capture groups can be called via $+{name}

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 Perl, please visit: http://perldoc.perl.org/perlre.html