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

use strict; my $str = ' HS codes: 271012 (Petroleum oils and oils obtained from bituminous minerals, other than crude; preparations not elsewhere specified or included, containing by weight 70 % or more of petroleum oils or of oils obtained from bituminous minerals, these oils being the basic constituents of the preparations; 33.234-33.237 waste oils : Petroleum oils and oils obtained from bituminous minerals (other than crude) and preparations not elsewhere specified or included, containing by weight 70 % or more of petroleum oils or of oils obtained from bituminous minerals, these oils being the basic constituents of the preparations, other than those containinandandandg biodiesel and other than waste oils : Light oils and preparations), 271019 (Petroleum oils and oils obtained from bituminous minerals, other than crude; preparations not elsewhere specified or included, containing by weight 70 % or more of petroleum oils or of oils obtained from bituminous minerals, these oils being the basic constituents of the preparations; waste oils : Petroleum oils and oils obtained from bituminous minerals (other than crude) and preparations not elsewhere specified or included, containing by weight 70 % or more of petroleum oils or of oils obtained from bituminous minerals, these oils being the basic constituents of the preparations, other than those containing biodiesel and other than waste oils : Other); ICS codes: 03.120 (Quality), 13.020 (Environmental protection), 75.160 (Fuels);HS 1234 2134 5325 ösadlkjföasfas HS: 8418 694X HS: 8415 10XX HS: 8418 694X HS: 8415 10XX HS 8418 101X, 8418 21XX ICS codes: 23.120 (Ventilators. Fans. Air-conditioners), 27.200 (Refrigerating technology), 71.100.45 (Refrigerants and antifreezes);ICS codes: 23.120 (Ventilators. Fans. Air-conditioners), 27.200 (Refrigerating technology), 71.100.45 (Refrigerants and antifreezes); ICS codes: 43.040.65 (Glazing and wiper systems) HS codes: 39.26.90.90.90 70.07.11.00.00 70.07.21.00.00 234 87.08.29.50.00 ICS-Codes: 83.160 (Tyres) (HS 4011). Electrical and electronic equipment (EEE). More detail on product coverage can be found in Restriction of the Use of Certain Hazardous Substances in Electrical and Electro 23XX n ic Equipment Regulations 2012 (SI 2012/3032) (https://www.legislation.gov.uk/uksi/2012/3032/made). HS code 9018 for medical devices. Electronic Components in general (ICS 31.020); Electronic components in general (ICS 31.020) Air emission sources; Quality (ICS code(s): 03.120); Environmental protection (ICS code(s): 13.020); Air quality (ICS code(s): 13.040); Test conditions and procedures in general (ICS code(s): 19.020) Passenger car (including New Energy Vehicle) (HS code(s): 8703); (ICS code(s): 43.060; 43.080.01; 43.100; 75.160) ICS codes: 29, 33, 97, 29 , 33, 97; 75.16X 13 13.123.34-34'; my $regex = qr/[^\d\.](((01|03|07|11|13|17|19|21|23|25|27|29|31|33|35|37|39|43|45|47|49|53|55|59|61|65|67|71|73|75|77|79|81|83|85|87|91|93|95|97|99)[\. ]?[\dX]{3}[\. ]?[\dX]{2}[\. \-]?[\dX]{2}|(01|03|07|11|13|17|19|21|23|25|27|29|31|33|35|37|39|43|45|47|49|53|55|59|61|65|67|71|73|75|77|79|81|83|85|87|91|93|95|97|99)[\. ]?[\dX]{3}[\. ]?[\dX]{2}|(01|03|07|11|13|17|19|21|23|25|27|29|31|33|35|37|39|43|45|47|49|53|55|59|61|65|67|71|73|75|77|79|81|83|85|87|91|93|95|97|99)[\. ]?[\dX]{3})(?![\d\.X]))|([^\d\.](01|03|07|11|13|17|19|21|23|25|27|29|31|33|35|37|39|43|45|47|49|53|55|59|61|65|67|71|73|75|77|79|81|83|85|87|91|93|95|97|99)(?![\d\.X]|( ?%)))/mp; if ( $str =~ /$regex/g ) { 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