Regular Expressions 101

Save & Manage Regex

  • Current Version: 1
  • Save & Share
  • Community Library

Flavor

  • PCRE2 (PHP)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java
  • .NET 7.0 (C#)
  • Rust
  • PCRE (Legacy)
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests
Sponsors
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
Processing...

Test String

Code Generator

Generated Code

import re regex = re.compile(r"(Ethernet [^a]*adapter) (Local[^:]+):\n*(( +[^\n]+\n)*)") test_str = ("\n" "Windows IP Configuration\n\n" " Host Name . . . . . . . . . . . . : Andrew-PC\n" " Primary Dns Suffix . . . . . . . : \n" " Node Type . . . . . . . . . . . . : Hybrid\n" " IP Routing Enabled. . . . . . . . : No\n" " WINS Proxy Enabled. . . . . . . . : No\n\n" "Ethernet adapter Local Area Connection:\n\n" " Connection-specific DNS Suffix . : \n" " Description . . . . . . . . . . . : Realtek PCIe GBE Family Controller\n" " Physical Address. . . . . . . . . : 00-24-1D-87-DE-E8\n" " DHCP Enabled. . . . . . . . . . . : No\n" " Autoconfiguration Enabled . . . . : Yes\n" " Link-local IPv6 Address . . . . . : fe80::2d30:3157:776c:5d1%3(Preferred) \n" " IPv4 Address. . . . . . . . . . . : 192.168.1.111(Preferred) \n" " Subnet Mask . . . . . . . . . . . : 255.255.255.0\n" " Default Gateway . . . . . . . . . : 192.168.1.1\n" " DHCPv6 IAID . . . . . . . . . . . : 234890269\n" " DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-1C-A8-DF-09-00-24-1D-87-DE-E6\n" " DNS Servers . . . . . . . . . . . : 75.75.75.75\n" " 75.75.76.76\n" " NetBIOS over Tcpip. . . . . . . . : Enabled\n\n" "Ethernet adapter VMware Network Adapter VMnet1:\n\n" " Connection-specific DNS Suffix . : \n" " Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet1\n" " Physical Address. . . . . . . . . : 00-50-56-C0-00-01\n" " DHCP Enabled. . . . . . . . . . . : Yes\n" " Autoconfiguration Enabled . . . . : Yes\n" " Link-local IPv6 Address . . . . . : fe80::7877:2883:1349:6d38%10(Preferred) \n" " IPv4 Address. . . . . . . . . . . : 192.168.107.1(Preferred) \n" " Subnet Mask . . . . . . . . . . . : 255.255.255.0\n" " Lease Obtained. . . . . . . . . . : Friday, February 26, 2016 7:31:41 AM\n" " Lease Expires . . . . . . . . . . : Friday, February 26, 2016 11:31:41 AM\n" " Default Gateway . . . . . . . . . : \n" " DHCP Server . . . . . . . . . . . : 192.168.107.254\n" " DHCPv6 IAID . . . . . . . . . . . : 167792726\n" " DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-1C-A8-DF-09-00-24-1D-87-DE-E6\n" " DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1\n" " fec0:0:0:ffff::2%1\n" " fec0:0:0:ffff::3%1\n" " NetBIOS over Tcpip. . . . . . . . : Enabled\n\n" "Ethernet adapter VMware Network Adapter VMnet8:\n\n" " Connection-specific DNS Suffix . : \n" " Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet8\n" " Physical Address. . . . . . . . . : 00-50-56-C0-00-08\n" " DHCP Enabled. . . . . . . . . . . : Yes\n" " Autoconfiguration Enabled . . . . : Yes\n" " Link-local IPv6 Address . . . . . : fe80::c57b:b161:5ed3:76ae%12(Preferred) \n" " IPv4 Address. . . . . . . . . . . : 192.168.88.1(Preferred) \n" " Subnet Mask . . . . . . . . . . . : 255.255.255.0\n" " Lease Obtained. . . . . . . . . . : Friday, February 26, 2016 7:31:34 AM\n" " Lease Expires . . . . . . . . . . : Friday, February 26, 2016 11:31:33 AM\n" " Default Gateway . . . . . . . . . : \n" " DHCP Server . . . . . . . . . . . : 192.168.88.254\n" " DHCPv6 IAID . . . . . . . . . . . : 201347158\n" " DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-1C-A8-DF-09-00-24-1D-87-DE-E6\n" " DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1\n" " fec0:0:0:ffff::2%1\n" " fec0:0:0:ffff::3%1\n" " Primary WINS Server . . . . . . . : 192.168.88.2\n" " NetBIOS over Tcpip. . . . . . . . : Enabled\n\n" "Tunnel adapter isatap.{9C26E507-7425-4DAB-BAC3-1E30E266D4DC}:\n\n" " Media State . . . . . . . . . . . : Media disconnected\n" " Connection-specific DNS Suffix . : \n" " Description . . . . . . . . . . . : Microsoft ISATAP Adapter\n" " Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0\n" " DHCP Enabled. . . . . . . . . . . : No\n" " Autoconfiguration Enabled . . . . : Yes\n\n" "Tunnel adapter Teredo Tunneling Pseudo-Interface:\n\n" " Connection-specific DNS Suffix . : \n" " Description . . . . . . . . . . . : Teredo Tunneling Pseudo-Interface\n" " Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0\n" " DHCP Enabled. . . . . . . . . . . : No\n" " Autoconfiguration Enabled . . . . : Yes\n" " IPv6 Address. . . . . . . . . . . : 2001:0:9d38:6ab8:c9e:283d:9d3b:997e(Preferred) \n" " Link-local IPv6 Address . . . . . : fe80::c9e:283d:9d3b:997e%2(Preferred) \n" " Default Gateway . . . . . . . . . : ::\n" " DHCPv6 IAID . . . . . . . . . . . : 150994944\n" " DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-1C-A8-DF-09-00-24-1D-87-DE-E6\n" " NetBIOS over Tcpip. . . . . . . . : Disabled\n\n" "Tunnel adapter isatap.{403315D9-A3E6-46A0-8CA5-6372C24FF823}:\n\n" " Media State . . . . . . . . . . . : Media disconnected\n" " Connection-specific DNS Suffix . : \n" " Description . . . . . . . . . . . : Microsoft ISATAP Adapter #2\n" " Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0\n" " DHCP Enabled. . . . . . . . . . . : No\n" " Autoconfiguration Enabled . . . . : Yes\n\n" "Tunnel adapter isatap.{B5708232-0F11-410B-930D-39D3A7908EAF}:\n\n" " Media State . . . . . . . . . . . : Media disconnected\n" " Connection-specific DNS Suffix . : \n" " Description . . . . . . . . . . . : Microsoft ISATAP Adapter #3\n" " Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0\n" " DHCP Enabled. . . . . . . . . . . : No\n" " Autoconfiguration Enabled . . . . : Yes\n\n" "Windows IP Configuration\n\n" " Host Name . . . . . . . . . . . . : Landen-Laptop\n" " Primary Dns Suffix . . . . . . . : \n" " Node Type . . . . . . . . . . . . : Hybrid\n" " IP Routing Enabled. . . . . . . . : No\n" " WINS Proxy Enabled. . . . . . . . : No\n\n" "Wireless LAN adapter Wireless Network Connection:\n\n" " Media State . . . . . . . . . . . : Media disconnected\n" " Connection-specific DNS Suffix . : \n" " Description . . . . . . . . . . . : Broadcom 802.11n Network Adapter\n" " Physical Address. . . . . . . . . : 00-1B-B1-AB-7E-74\n" " DHCP Enabled. . . . . . . . . . . : Yes\n" " Autoconfiguration Enabled . . . . : Yes\n\n" "Ethernet adapter Local Area Connection:\n\n" " Connection-specific DNS Suffix . : \n" " Description . . . . . . . . . . . : Realtek PCIe GBE Family Controller\n" " Physical Address. . . . . . . . . : E8-11-32-2E-26-23\n" " DHCP Enabled. . . . . . . . . . . : Yes\n" " Autoconfiguration Enabled . . . . : Yes\n" " Link-local IPv6 Address . . . . . : fe80::edba:a079:1244:b4b3%13(Preferred) \n" " IPv4 Address. . . . . . . . . . . : 192.168.0.106(Preferred) \n" " Subnet Mask . . . . . . . . . . . : 255.255.255.0\n" " Lease Obtained. . . . . . . . . . : Friday, February 26, 2016 10:40:56 AM\n" " Lease Expires . . . . . . . . . . : Friday, February 26, 2016 12:40:55 PM\n" " Default Gateway . . . . . . . . . : 192.168.0.11\n" " DHCP Server . . . . . . . . . . . : 192.168.0.11\n" " DHCPv6 IAID . . . . . . . . . . . : 300421426\n" " DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-1E-50-43-28-E8-11-32-2E-26-22\n" " DNS Servers . . . . . . . . . . . : 208.67.222.123\n" " 208.67.220.123\n" " NetBIOS over Tcpip. . . . . . . . : Enabled\n\n" "Ethernet adapter Bluetooth Network Connection:\n\n" " Media State . . . . . . . . . . . : Media disconnected\n" " Connection-specific DNS Suffix . : \n" " Description . . . . . . . . . . . : Bluetooth Device (Personal Area Network)\n" " Physical Address. . . . . . . . . : 00-1B-B1-FB-34-76\n" " DHCP Enabled. . . . . . . . . . . : Yes\n" " Autoconfiguration Enabled . . . . : Yes\n\n" "Tunnel adapter isatap.{1F3D3DAD-1D52-4329-9C52-58CD7B54C86A}:\n\n" " Media State . . . . . . . . . . . : Media disconnected\n" " Connection-specific DNS Suffix . : \n" " Description . . . . . . . . . . . : Microsoft ISATAP Adapter\n" " Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0\n" " DHCP Enabled. . . . . . . . . . . : No\n" " Autoconfiguration Enabled . . . . : Yes\n\n" "Tunnel adapter isatap.{25CBD760-74ED-4CE7-8E1B-328A12335E7A}:\n\n" " Media State . . . . . . . . . . . : Media disconnected\n" " Connection-specific DNS Suffix . : \n" " Description . . . . . . . . . . . : Microsoft ISATAP Adapter #2\n" " Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0\n" " DHCP Enabled. . . . . . . . . . . : No\n" " Autoconfiguration Enabled . . . . : Yes\n\n" "Tunnel adapter isatap.{4AC4D1F9-A993-4CBE-9CB8-414BD6853831}:\n\n" " Media State . . . . . . . . . . . : Media disconnected\n" " Connection-specific DNS Suffix . : \n" " Description . . . . . . . . . . . : Microsoft ISATAP Adapter #3\n" " Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0\n" " DHCP Enabled. . . . . . . . . . . : No\n" " Autoconfiguration Enabled . . . . : Yes\n\n" "Windows IP Configuration\n" " Host Name . . . . . . . . . . . . : PChuck1\n" " Primary Dns Suffix . . . . . . . : \n" " Node Type . . . . . . . . . . . . : Broadcast\n" " IP Routing Enabled. . . . . . . . : No\n" " WINS Proxy Enabled. . . . . . . . : No\n" " DNS Suffix Search List. . . . . . : pchuck.net\n\n" "Ethernet adapter Local Area Connection:\n" " Connection-specific DNS Suffix . : \n" " Description . . . . . . . . . . . : 3Com EtherLink XL 10/100 PCI For Complete PC Management NIC (3C905C-TX)\n" " Physical Address. . . . . . . . . : 00-04-76-D7-C5-6B\n" " Dhcp Enabled. . . . . . . . . . . : Yes\n" " Autoconfiguration Enabled . . . . : Yes\n" " IP Address. . . . . . . . . . . . : 192.168.1.59\n" " Subnet Mask . . . . . . . . . . . : 255.255.255.0\n" " Default Gateway . . . . . . . . . : 192.168.1.1\n" " DHCP Server . . . . . . . . . . . : 192.168.1.1\n" " DNS Servers . . . . . . . . . . . : 192.168.1.11\n" " 192.168.1.33\n" " Lease Obtained. . . . . . . . . . : Wednesday, April 16, 2003 11:19:12\n" " Lease Expires . . . . . . . . . . : Wednesday, April 23, 2003 11:19:12\n\n" "Windows IP Configuration\n" " Host Name . . . . . . . . . . . . : PChuck2\n" " Primary Dns Suffix . . . . . . . : \n" " Node Type . . . . . . . . . . . . : Hybrid\n" " IP Routing Enabled. . . . . . . . : No\n" " WINS Proxy Enabled. . . . . . . . : No\n" " DNS Suffix Search List. . . . . . : pchuck.net\n" "Ethernet adapter Local Area Connection:\n" " Connection-specific DNS Suffix . : \n" " Description . . . . . . . . . . . : 3Com EtherLink XL 10/100 PCI For Complete PC Management NIC (3C905C-TX)\n" " Physical Address. . . . . . . . . : 00-04-76-D7-76-BC\n" " Dhcp Enabled. . . . . . . . . . . : Yes\n" " Autoconfiguration Enabled . . . . : Yes\n" " IP Address. . . . . . . . . . . . : 192.168.1.51\n" " Subnet Mask . . . . . . . . . . . : 255.255.255.0\n" " Default Gateway . . . . . . . . . : 192.168.1.1\n" " DHCP Server . . . . . . . . . . . : 192.168.1.1\n" " DNS Servers . . . . . . . . . . . : 192.168.1.11\n" " 192.168.1.33\n" " Primary WINS Server . . . . . . . : 192.168.1.1\n" " Lease Obtained. . . . . . . . . . : Wednesday, April 16, 2003 11:53:45\n" " Lease Expires . . . . . . . . . . : Wednesday, April 23, 2003 11:53:45") matches = regex.finditer(test_str) for match_num, match in enumerate(matches, start=1): print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}") for group_num, group in enumerate(match.groups(), start=1): print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")

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