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

/
/
gm

Test String

Code Generator

Generated Code

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"^Cisco Catalyst.*AC Power Supply$" test_str = ("Cisco Catalyst 9500 Series Chassis\n" "Cisco Catalyst 9500 Series Power Supply Bay Module Container\n" "Cisco Catalyst 9500 Series Power Supply Bay Module Container\n" "Cisco Catalyst 9500 Series Fan Tray Bay Module Container\n" "Cisco Catalyst 9500 Series 650W AC Power Supply\n" "Cisco Catalyst 9500 Series Power Supply\n" "Cisco Catalyst 9500 Series Fan\n" "Cisco Catalyst 9500 Series 650W AC Power Supply\n" "Cisco Catalyst 9500 Series Power Supply\n" "Cisco Catalyst 9500 Series Fan\n" "Cisco Catalyst 9500 Series Fan Tray\n" "Cisco Catalyst 9500 Series Fan Tray\n" "Cisco Catalyst 9500 Series Fan\n" "Cisco Catalyst 9500 Series Fan\n" "Cisco Catalyst 9500 Series Fan\n" "Cisco Catalyst 9500 Series Fan\n" "Cisco Catalyst 9500 Series Fan\n" "Cisco Catalyst 9500 Series Fan\n" "Cisco Catalyst 9500 Series Fan\n" "Cisco Catalyst 9500 Series Fan\n" "Cisco Catalyst 9500 Series Router\n" "PSOC-MB_0: VOU\n" "PSOC-MB_1: VOU\n" "PSOC-MB_2: VOU\n" "PSOC-MB_3: VOU\n" "PSOC-MB_4: VOU\n" "PSOC-MB_5: VOU\n" "PSOC-MB_7: VOU\n" "PSOC-MB_8: VOU\n" "PSOC-MB_9: VOU\n" "PSOC-MB_10: VO\n" "3570MB1_0: VOU\n" "3570MB2_0: VOU\n" "3570MB3_0: VOU\n" "35215MB1_0: VO\n" "35215MB2_0: VO\n" "Temp: Outlet_A\n" "Temp: Outlet_B\n" "Temp: Inlet_A\n" "Temp: Inlet_B\n" "Temp: UADP_0_0\n" "Temp: UADP_0_1\n" "Temp: UADP_0_2\n" "Temp: UADP_0_3\n" "Temp: UADP_0_4\n" "Temp: UADP_0_5\n" "Temp: UADP_0_6\n" "Temp: UADP_0_7\n" "Temp: UADP_0_8\n" "PSOC-DB_1: VOU\n" "PSOC-DB_2: VOU\n" "PSOC-DB_4: VOU\n" "PSOC-DB_5: VOU\n" "PSOC-DB_7: VOU\n" "PSOC-DB_8: VOU\n" "PSOC-DB_11: VO\n" "3570DB1_0: VOU\n" "3570DB2_0: VOU\n" "3570DB3_0: VOU\n" "Temp: Coretemp\n" "Temp: OutletDB\n" "Intel CPU x86-64\n" "USB Port\n" "TwentyFiveGigE1/0/1 Container\n" "TwentyFiveGigE1/0/2 Container\n" "TwentyFiveGigE1/0/3 Container\n" "TwentyFiveGigE1/0/4 Container\n" "TwentyFiveGigE1/0/5 Container\n" "TwentyFiveGigE1/0/6 Container\n" "TwentyFiveGigE1/0/7 Container\n" "TwentyFiveGigE1/0/8 Container\n" "TwentyFiveGigE1/0/9 Container\n" "TwentyFiveGigE1/0/10 Container\n" "TwentyFiveGigE1/0/11 Container\n" "TwentyFiveGigE1/0/12 Container\n" "TwentyFiveGigE1/0/13 Container\n" "TwentyFiveGigE1/0/14 Container\n" "TwentyFiveGigE1/0/15 Container\n" "TwentyFiveGigE1/0/16 Container\n" "TwentyFiveGigE1/0/17 Container\n" "TwentyFiveGigE1/0/18 Container\n" "TwentyFiveGigE1/0/19 Container\n" "TwentyFiveGigE1/0/20 Container\n" "TwentyFiveGigE1/0/21 Container\n" "TwentyFiveGigE1/0/22 Container\n" "TwentyFiveGigE1/0/23 Container\n" "TwentyFiveGigE1/0/24 Container\n" "HundredGigE1/0/25 Container\n" "HundredGigE1/0/26 Container\n" "HundredGigE1/0/27 Container\n" "HundredGigE1/0/28 Container\n" "SFP+ 10GBASE-SR\n" "Twe1/0/1 Module Temperature Sensor\n" "Twe1/0/1 Supply Voltage Sensor\n" "Twe1/0/1 Bias Current Sensor\n" "Twe1/0/1 Transmit Power Sensor\n" "Twe1/0/1 Receive Power Sensor\n" "GE T\n" "GE T\n" "SFP+ 10GBASE-SR") matches = re.finditer(regex, test_str, re.MULTILINE) for matchNum, match in enumerate(matches, start=1): print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group())) for groupNum in range(0, len(match.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum))) # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

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 Python, please visit: https://docs.python.org/3/library/re.html