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
  • 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

/
/
gm

Test String

Code Generator

Generated Code

use strict; my $str = 'CPU1 Temp | 0x0 | discrete | 0x0000| na | na | na | na | na | na CPU2 Temp | 0x0 | discrete | 0x0000| na | na | na | na | na | na System Temp | 35,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 75,000 | 77,000 | 79,000 CPU1 Vcore | 1,200 | Volts | ok | 0,656 | 0,664 | 0,672 | 1,352 | 1,360 | 1,368 CPU2 Vcore | 0,936 | Volts | ok | 0,656 | 0,664 | 0,672 | 1,352 | 1,360 | 1,368 CPU1 DIMM | 1,520 | Volts | ok | 1,184 | 1,192 | 1,200 | 1,648 | 1,656 | 1,664 CPU2 DIMM | 1,520 | Volts | ok | 1,184 | 1,192 | 1,200 | 1,648 | 1,656 | 1,664 +1.5V | 1,504 | Volts | ok | 1,320 | 1,328 | 1,336 | 1,656 | 1,664 | 1,672 +3.3V | 3,240 | Volts | ok | 2,880 | 2,904 | 2,928 | 3,648 | 3,672 | 3,696 +3.3VSB | 3,216 | Volts | ok | 2,880 | 2,904 | 2,928 | 3,648 | 3,672 | 3,696 +5V | 5,056 | Volts | ok | 4,416 | 4,448 | 4,480 | 5,536 | 5,568 | 5,600 +12V | 12,137 | Volts | ok | 10,600 | 10,653 | 10,706 | 13,250 | 13,303 | 13,356 VBAT | 3,192 | Volts | ok | 2,880 | 2,904 | 2,928 | 3,648 | 3,672 | 3,696 Fan1 | na | RPM | na | 405,000 | 540,000 | 675,000 | 34155,000 | 34290,000 | 34425,000 Fan2 | na | RPM | na | 405,000 | 540,000 | 675,000 | 34155,000 | 34290,000 | 34425,000 Fan3 | na | RPM | na | 405,000 | 540,000 | 675,000 | 34155,000 | 34290,000 | 34425,000 Fan4 | na | RPM | na | 405,000 | 540,000 | 675,000 | 34155,000 | 34290,000 | 34425,000 Fan5 | na | RPM | na | 405,000 | 540,000 | 675,000 | 34155,000 | 34290,000 | 34425,000 Fan6 | na | RPM | na | 405,000 | 540,000 | 675,000 | 34155,000 | 34290,000 | 34425,000 Fan7 | na | RPM | na | 405,000 | 540,000 | 675,000 | 34155,000 | 34290,000 | 34425,000 Fan8 | na | RPM | na | 405,000 | 540,000 | 675,000 | 34155,000 | 34290,000 | 34425,000 P1-DIMM1A Temp | 27,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 P1-DIMM1B Temp | 26,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 P1-DIMM2A Temp | 28,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 P1-DIMM2B Temp | 28,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 P1-DIMM3A Temp | 29,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 P1-DIMM3B Temp | 29,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 P2-DIMM1A Temp | 27,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 P2-DIMM1B Temp | 26,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 P2-DIMM2A Temp | 28,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 P2-DIMM2B Temp | 28,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 P2-DIMM3A Temp | 29,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 P2-DIMM3B Temp | 29,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 Intrusion | 0x1 | discrete | 0x0100| na | na | na | na | na | na PS Status | 0x1 | discrete | 0x0100| na | na | na | na | na | na '; my $regex = qr/^\s*(?<name>[^|]+?)\s*\|\s*(?<value>[^|]+?)\s*\|\s*(?<unit>[^|]+?)\s*\|\s*(?<status>[^|]+?)\s*\|\s*(?<type>[^|]+?)\s*\|\s*(?<state>[^|]+?)\s*\|\s*(?<lower_nonrec>[^|]+?)\s*\|\s*(?<lower_crit>[^|]+?)\s*\|\s*(?<lower_noncrit>[^|]+?)\s*\|\s*(?<upper_crit>[^|]+?)\s*$/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