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
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
Processing...

Test String

Code Generator

Generated Code

import re regex = re.compile(r"^ ([\w -_ ]+):\n\n +Type: Ethernet\n +Hardware.+\n +BSD Device Name: (.+)\n +IPv4 Addresses: (192[\.\d]+)$", flags=re.MULTILINE) test_str = ("Network:\n\n" " USB 10/100/1000 LAN 2:\n\n" " Type: Ethernet\n" " Hardware: Ethernet\n" " BSD Device Name: en6\n" " IPv4 Addresses: 192.168.136.251\n" " IPv4:\n" " AdditionalRoutes:\n" " DestinationAddress: 192.168.136.251\n" " SubnetMask: 255.255.255.255\n" " DestinationAddress: 169.254.0.0\n" " SubnetMask: 255.255.0.0\n" " Addresses: 192.168.136.251\n" " ARPResolvedHardwareAddress: f6:92:bf:88:c8:2e\n" " ARPResolvedIPAddress: 192.168.136.1\n" " Configuration Method: INFORM\n" " ConfirmedInterfaceName: en6\n" " Interface Name: en6\n" " Network Signature: IPv4.Router=192.168.136.1;IPv4.RouterHardwareAddress=f6:92:bf:88:c8:2e\n" " Router: 192.168.136.1\n" " Subnet Masks: 255.255.255.255\n" " IPv6:\n" " Addresses: 2601:283:4200:d0a:42f:af72:ae41:3c8f, 2601:283:4200:d0a:bdb9:1223:3c59:f741, fd7b:2874:64bb:10:c0c:5dc1:be14:6d7d, fd7b:2874:64bb:10:a583:5011:61f6:cb0d\n" " Configuration Method: Automatic\n" " ConfirmedInterfaceName: en6\n" " Interface Name: en6\n" " Network Signature: IPv6.Prefix=2601:283:4200:d0a::/64;IPv6.RouterHardwareAddress=f6:92:bf:88:c8:2e\n" " Prefix Length: 64, 64, 64, 64\n" " Router: fe80::f492:bfff:fe88:c82e\n" " DNS:\n" " Server Addresses: 192.168.136.1, fd7b:2874:64bb:10::1\n" " DHCP Server Responses:\n" " Domain Name Servers: 192.168.136.1\n" " DHCP Message Type: 0x05\n" " Routers: 192.168.136.1\n" " Server Identifier: 192.168.136.1\n" " Ethernet:\n" " MAC Address: 00:e0:4c:68:0f:96\n" " Media Options: Full Duplex\n" " Media Subtype: 1000baseT\n" " Proxies:\n" " Exceptions List: *.local, 169.254/16\n" " FTP Passive Mode: Yes\n" " Service Order: 0\n\n" " USB 10/100/1000 LAN:\n\n" " Type: Ethernet\n" " Hardware: Ethernet\n" " BSD Device Name: en7\n" " IPv4:\n" " Configuration Method: DHCP\n" " IPv6:\n" " Configuration Method: Automatic\n" " Proxies:\n" " Exceptions List: *.local, 169.254/16\n" " FTP Passive Mode: Yes\n" " Service Order: 1\n\n" " USB Ethernet:\n\n" " Type: Ethernet\n" " Hardware: Ethernet\n" " BSD Device Name: en7\n" " IPv4 Addresses: 10.10.10.6\n" " IPv4:\n" " Addresses: 10.10.10.6\n" " Configuration Method: Manual\n" " Subnet Masks: 255.255.0.0\n" " IPv6:\n" " Configuration Method: Automatic\n" " DNS:\n" " Server Addresses: 1.1.1.1, 1.0.0.1, 2606:4700:4700::1111, 2606:4700:4700::1111\n" " Proxies:\n" " Exceptions List: *.local, 169.254/16\n" " FTP Passive Mode: Yes\n" " Service Order: 2\n\n" " USB 10/100/1000 LAN 3:\n\n" " Type: Ethernet\n" " Hardware: Ethernet\n" " BSD Device Name: en11\n" " IPv4:\n" " Configuration Method: DHCP\n" " IPv6:\n" " Configuration Method: Automatic\n" " Proxies:\n" " Exceptions List: *.local, 169.254/16\n" " FTP Passive Mode: Yes\n" " Service Order: 3\n\n" " Wi-Fi:\n\n" " Type: AirPort\n" " Hardware: AirPort\n" " BSD Device Name: en0\n" " IPv4 Addresses: 192.168.136.184\n" " IPv4:\n" " AdditionalRoutes:\n" " DestinationAddress: 192.168.136.184\n" " SubnetMask: 255.255.255.255\n" " DestinationAddress: 169.254.0.0\n" " SubnetMask: 255.255.0.0\n" " Addresses: 192.168.136.184\n" " ARPResolvedHardwareAddress: f6:92:bf:88:c8:2e\n" " ARPResolvedIPAddress: 192.168.136.1\n" " Configuration Method: DHCP\n" " ConfirmedInterfaceName: en0\n" " Interface Name: en0\n" " Network Signature: IPv4.Router=192.168.136.1;IPv4.RouterHardwareAddress=f6:92:bf:88:c8:2e\n" " Router: 192.168.136.1\n" " Subnet Masks: 255.255.255.0\n" " IPv6:\n" " Addresses: 2601:283:4200:d0a:1849:f70b:a649:aaa1, 2601:283:4200:d0a:7088:d9:fe7e:80e8, fd7b:2874:64bb:10:2d:2f19:470f:32d1, fd7b:2874:64bb:10:a12f:2f04:f8c:1cbb\n" " Configuration Method: Automatic\n" " ConfirmedInterfaceName: en0\n" " Interface Name: en0\n" " Network Signature: IPv6.Prefix=2601:283:4200:d0a::/64;IPv6.RouterHardwareAddress=f6:92:bf:88:c8:2e\n" " Prefix Length: 64, 64, 64, 64\n" " Router: fe80::f492:bfff:fe88:c82e\n" " DNS:\n" " Server Addresses: 192.168.136.1, fd7b:2874:64bb:10::1\n" " DHCP Server Responses:\n" " Domain Name Servers: 192.168.136.1\n" " Lease Duration (seconds): 0\n" " DHCP Message Type: 0x05\n" " Routers: 192.168.136.1\n" " Server Identifier: 192.168.136.1\n" " Subnet Mask: 255.255.255.0\n" " Ethernet:\n" " MAC Address: f8:ff:c2:67:29:32\n" " Media Options: \n" " Media Subtype: Auto Select\n" " Proxies:\n" " Exceptions List: *.local, 169.254/16\n" " FTP Passive Mode: Yes\n" " Sleep Proxies:\n" " 1 m&m tv:\n" " Marginal Power: 60\n" " Metric: 703560\n" " Portability: 35\n" " Total Power: 63\n" " Type: 70\n" " 1 workout room:\n" " Marginal Power: 60\n" " Metric: 703560\n" " Portability: 35\n" " Total Power: 63\n" " Type: 70\n" " Service Order: 4\n\n" " SparkFun Pro Micro:\n\n" " Type: PPP (PPPSerial)\n" " Hardware: Modem\n" " BSD Device Name: usbmodem14131\n" " IPv4:\n" " Configuration Method: PPP\n" " IPv6:\n" " Configuration Method: Automatic\n" " Proxies:\n" " FTP Passive Mode: Yes\n" " Service Order: 5\n\n" " Display Ethernet:\n\n" " Type: Ethernet\n" " Hardware: Ethernet\n" " BSD Device Name: en9\n" " IPv4 Addresses: 10.10.10.1\n" " IPv4:\n" " Addresses: 10.10.10.1\n" " Configuration Method: Manual\n" " Subnet Masks: 255.255.0.0\n" " IPv6:\n" " Configuration Method: Automatic\n" " Ethernet:\n" " MAC Address: 0c:4d:e9:ae:d3:b3\n" " Media Options: \n" " Media Subtype: none\n" " Proxies:\n" " Exceptions List: *.local, 169.254/16\n" " FTP Passive Mode: Yes\n" " Service Order: 6\n\n" " Thunderbolt Ethernet:\n\n" " Type: Ethernet\n" " Hardware: Ethernet\n" " BSD Device Name: en12\n" " IPv4 Addresses: 10.10.10.3\n" " IPv4:\n" " Addresses: 10.10.10.3\n" " Configuration Method: Manual\n" " Subnet Masks: 255.255.255.0\n" " IPv6:\n" " Configuration Method: Automatic\n" " Proxies:\n" " Exceptions List: *.local, 169.254/16\n" " FTP Passive Mode: Yes\n" " Service Order: 7\n\n" " Thunderbolt Ethernet Slot 1, Port 1:\n\n" " Type: Ethernet\n" " Hardware: Ethernet\n" " BSD Device Name: en10\n" " IPv4:\n" " Configuration Method: DHCP\n" " IPv6:\n" " Configuration Method: Automatic\n" " Ethernet:\n" " MAC Address: 64:4b:f0:19:ac:9c\n" " Media Options: \n" " Media Subtype: Auto Select\n" " Proxies:\n" " Exceptions List: *.local, 169.254/16\n" " FTP Passive Mode: Yes\n" " Service Order: 8\n\n" " Display FireWire:\n\n" " Type: FireWire\n" " Hardware: FireWire\n" " BSD Device Name: fw0\n" " IPv4:\n" " Configuration Method: DHCP\n" " IPv6:\n" " Configuration Method: Automatic\n" " Ethernet:\n" " MAC Address: 00:0a:27:02:00:5d:2a:82\n" " Media Options: Full Duplex\n" " Media Subtype: Auto Select\n" " Proxies:\n" " Exceptions List: *.local, 169.254/16\n" " FTP Passive Mode: Yes\n" " Service Order: 9\n\n" " Bluetooth PAN 2:\n\n" " Type: Ethernet\n" " Hardware: Ethernet\n" " BSD Device Name: en8\n" " IPv4:\n" " Configuration Method: DHCP\n" " IPv6:\n" " Configuration Method: Automatic\n" " Proxies:\n" " Exceptions List: *.local, 169.254/16\n" " FTP Passive Mode: Yes\n" " Service Order: 10\n\n" " Thunderbolt Bridge:\n\n" " Type: Ethernet\n" " Hardware: Ethernet\n" " BSD Device Name: bridge0\n" " IPv4 Addresses: 10.10.10.2\n" " IPv4:\n" " Addresses: 10.10.10.2\n" " Configuration Method: Manual\n" " Subnet Masks: 255.0.0.0\n" " IPv6:\n" " Configuration Method: Link Local\n" " Proxies:\n" " Exceptions List: *.local, 169.254/16\n" " FTP Passive Mode: Yes\n" " Service Order: 11\n\n" " Thunderbolt FireWire:\n\n" " Type: FireWire\n" " Hardware: FireWire\n" " BSD Device Name: fw1\n" " IPv4:\n" " Configuration Method: DHCP\n" " IPv6:\n" " Configuration Method: Automatic\n" " Proxies:\n" " Exceptions List: *.local, 169.254/16\n" " FTP Passive Mode: Yes\n" " Service Order: 12\n\n" " Thunderbolt FireWire 2:\n\n" " Type: FireWire\n" " Hardware: FireWire\n" " BSD Device Name: fw2\n" " IPv4:\n" " Configuration Method: DHCP\n" " IPv6:\n" " Configuration Method: Automatic\n" " Proxies:\n" " Exceptions List: *.local, 169.254/16\n" " FTP Passive Mode: Yes\n" " Service Order: 13\n\n" " Thunderbolt Bridge 2:\n\n" " Type: Ethernet\n" " Hardware: Ethernet\n" " BSD Device Name: bridge0\n" " IPv4:\n" " Configuration Method: DHCP\n" " IPv6:\n" " Configuration Method: Automatic\n" " Proxies:\n" " Exceptions List: *.local, 169.254/16\n" " FTP Passive Mode: Yes\n" " Service Order: 14\n") 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