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

/
/
gim

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"([[:blank:]][[:xdigit:]]{2}-[[:xdigit:]]{2}-[[:xdigit:]]{2}-[[:xdigit:]]{2}-[[:xdigit:]]{2}-[[:xdigit:]]{2})$" test_str = ("Windows-IP-Konfiguration\n\n" " Hostname . . . . . . . . . . . . : tarox\n" " Primäres DNS-Suffix . . . . . . . :\n" " Knotentyp . . . . . . . . . . . . : Hybrid\n" " IP-Routing aktiviert . . . . . . : Nein\n" " WINS-Proxy aktiviert . . . . . . : Nein\n\n" "Ethernet-Adapter Bluetooth-Netzwerkverbindung:\n\n" " Medienstatus. . . . . . . . . . . : Medium getrennt\n" " Verbindungsspezifisches DNS-Suffix:\n" " Beschreibung. . . . . . . . . . . : Bluetooth-Gerät (PAN)\n" " Physikalische Adresse . . . . . . : 00-25-D3-B7-CF-FE\n" " DHCP aktiviert. . . . . . . . . . : Ja\n" " Autokonfiguration aktiviert . . . : Ja\n\n" "Ethernet-Adapter OpenVPN frenzy:\n\n" " Medienstatus. . . . . . . . . . . : Medium getrennt\n" " Verbindungsspezifisches DNS-Suffix:\n" " Beschreibung. . . . . . . . . . . : TAP-Windows Adapter V9 #2\n" " Physikalische Adresse . . . . . . : 00-FF-BE-A2-27-2D\n" " DHCP aktiviert. . . . . . . . . . : Ja\n" " Autokonfiguration aktiviert . . . : Ja\n\n" "Ethernet-Adapter OpenVPN megatron:\n\n" " Medienstatus. . . . . . . . . . . : Medium getrennt\n" " Verbindungsspezifisches DNS-Suffix:\n" " Beschreibung. . . . . . . . . . . : TAP-Windows Adapter V9\n" " Physikalische Adresse . . . . . . : 00-FF-5B-6B-65-09\n" " DHCP aktiviert. . . . . . . . . . : Ja\n" " Autokonfiguration aktiviert . . . : Ja\n\n" "Drahtlos-LAN-Adapter Drahtlosnetzwerkverbindung:\n\n" " Medienstatus. . . . . . . . . . . : Medium getrennt\n" " Verbindungsspezifisches DNS-Suffix: stollfuss.net\n" " Beschreibung. . . . . . . . . . . : Intel(R) Centrino(R) Wireless-N 1000\n" " Physikalische Adresse . . . . . . : 8C-A9-82-2E-0C-FE\n" " DHCP aktiviert. . . . . . . . . . : Ja\n" " Autokonfiguration aktiviert . . . : Ja\n\n" "Ethernet-Adapter LAN-Verbindung:\n\n" " Verbindungsspezifisches DNS-Suffix:\n" " Beschreibung. . . . . . . . . . . : Atheros AR8121/AR8113/AR8114 PCI-E Ethernet Controller(NDIS6.20)\n" " Physikalische Adresse . . . . . . : 70-71-BC-D6-BF-77\n" " DHCP aktiviert. . . . . . . . . . : Nein\n" " Autokonfiguration aktiviert . . . : Ja\n" " Verbindungslokale IPv6-Adresse . : fe80::8517:61e4:94f0:173a%11(Bevorzugt)\n" " IPv4-Adresse . . . . . . . . . . : 192.168.2.10(Bevorzugt)\n" " Subnetzmaske . . . . . . . . . . : 255.255.255.0\n" " Standardgateway . . . . . . . . . : 192.168.2.1\n" " DHCPv6-IAID . . . . . . . . . . . : 242250172\n" " DHCPv6-Client-DUID. . . . . . . . : 00-01-00-01-1C-64-1F-D5-70-71-BC-D6-BF-77\n" " DNS-Server . . . . . . . . . . . : 192.168.2.1\n" " NetBIOS über TCP/IP . . . . . . . : Aktiviert\n\n" "Tunneladapter isatap.stollfuss.net:\n\n" " Medienstatus. . . . . . . . . . . : Medium getrennt\n" " Verbindungsspezifisches DNS-Suffix:\n" " Beschreibung. . . . . . . . . . . : Microsoft-ISATAP-Adapter\n" " Physikalische Adresse . . . . . . : 00-00-00-00-00-00-00-E0\n" " DHCP aktiviert. . . . . . . . . . : Nein\n" " Autokonfiguration aktiviert . . . : Ja\n\n" "Tunneladapter LAN-Verbindung* 11:\n\n" " Medienstatus. . . . . . . . . . . : Medium getrennt\n" " Verbindungsspezifisches DNS-Suffix:\n" " Beschreibung. . . . . . . . . . . : Teredo Tunneling Pseudo-Interface\n" " Physikalische Adresse . . . . . . : 00-00-00-00-00-00-00-E0\n" " DHCP aktiviert. . . . . . . . . . : Nein\n" " Autokonfiguration aktiviert . . . : Ja\n\n" "Tunneladapter isatap.{5B6B6509-4089-4D8B-873E-6C9BCECA9C38}:\n\n" " Medienstatus. . . . . . . . . . . : Medium getrennt\n" " Verbindungsspezifisches DNS-Suffix:\n" " Beschreibung. . . . . . . . . . . : Microsoft-ISATAP-Adapter #2\n" " Physikalische Adresse . . . . . . : 00-00-00-00-00-00-00-E0\n" " DHCP aktiviert. . . . . . . . . . : Nein\n" " Autokonfiguration aktiviert . . . : Ja\n\n" "Tunneladapter Reusable ISATAP Interface {36BFBE43-5F0F-42E3-89B3-B581DF95C122}:\n\n" " Medienstatus. . . . . . . . . . . : Medium getrennt\n" " Verbindungsspezifisches DNS-Suffix:\n" " Beschreibung. . . . . . . . . . . : Microsoft-ISATAP-Adapter #3\n" " Physikalische Adresse . . . . . . : 00-00-00-00-00-00-00-E0\n" " DHCP aktiviert. . . . . . . . . . : Nein\n" " Autokonfiguration aktiviert . . . : Ja\n\n" "Tunneladapter isatap.{BE04D9C2-65F1-4777-9918-1CFEBAED5268}:\n\n" " Medienstatus. . . . . . . . . . . : Medium getrennt\n" " Verbindungsspezifisches DNS-Suffix:\n" " Beschreibung. . . . . . . . . . . : Microsoft-ISATAP-Adapter #4\n" " Physikalische Adresse . . . . . . : 00-00-00-00-00-00-00-E0\n" " DHCP aktiviert. . . . . . . . . . : Nein\n" " Autokonfiguration aktiviert . . . : Ja\n\n" "Tunneladapter Reusable ISATAP Interface {A887E6BC-C744-4C0E-A17A-70E04B3DC3E2}:\n\n" " Medienstatus. . . . . . . . . . . : Medium getrennt\n" " Verbindungsspezifisches DNS-Suffix:\n" " Beschreibung. . . . . . . . . . . : Microsoft-ISATAP-Adapter #5\n" " Physikalische Adresse . . . . . . : 00-00-00-00-00-00-00-E0\n" " DHCP aktiviert. . . . . . . . . . : Nein\n" " Autokonfiguration aktiviert . . . : Ja\n\n" "Tunneladapter isatap.{BEA2272D-AC7D-47E1-A3A4-D7273CF9CAFD}:\n\n" " Medienstatus. . . . . . . . . . . : Medium getrennt\n" " Verbindungsspezifisches DNS-Suffix:\n" " Beschreibung. . . . . . . . . . . : Microsoft-ISATAP-Adapter #6\n" " Physikalische Adresse . . . . . . : 00-00-00-00-00-00-00-E0\n" " DHCP aktiviert. . . . . . . . . . : Nein\n" " Autokonfiguration aktiviert . . . : Ja\n\n" "Tunneladapter isatap.{20DD8A4C-A552-43A5-9B81-ABA8D73754B3}:\n\n" " Medienstatus. . . . . . . . . . . : Medium getrennt\n" " Verbindungsspezifisches DNS-Suffix:\n" " Beschreibung. . . . . . . . . . . : Microsoft-ISATAP-Adapter #7\n" " Physikalische Adresse . . . . . . : 00-00-00-00-00-00-00-E0\n" " DHCP aktiviert. . . . . . . . . . : Nein\n" " Autokonfiguration aktiviert . . . : Ja") matches = re.finditer(regex, test_str, re.IGNORECASE | 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