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

$re = '/^ ([\w-_ ]+):\n\n +Type: AirPort\n +Hardware.+\n +BSD Device Name: (.+)\n +IPv4 Addresses: (192[\.\d]+)$/m'; $str = 'ETHERNET DISCONNECTED, WI-FI RUNNING ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Network: Thunderbolt Ethernet Slot 1: Type: Ethernet Hardware: Ethernet BSD Device Name: en8 IPv4: Configuration Method: DHCP IPv6: Configuration Method: Automatic Ethernet: MAC Address: 00:50:b6:ca:f0:dc Media Options: Media Subtype: Auto Select Proxies: Exceptions List: *.local, 169.254/16 FTP Passive Mode: Yes Service Order: 0 Thunderbolt Ethernet: Type: Ethernet Hardware: Ethernet BSD Device Name: en6 IPv4: Configuration Method: DHCP IPv6: Configuration Method: Automatic Proxies: Exceptions List: *.local, 169.254/16 FTP Passive Mode: Yes Service Order: 1 Wi-Fi: Type: AirPort Hardware: AirPort BSD Device Name: en0 IPv4 Addresses: 192.168.1.16 IPv4: AdditionalRoutes: DestinationAddress: 192.168.1.16 SubnetMask: 255.255.255.255 DestinationAddress: 169.254.0.0 SubnetMask: 255.255.0.0 Addresses: 192.168.1.16 ARPResolvedHardwareAddress: 2c:b0:5d:25:af:69 ARPResolvedIPAddress: 192.168.1.1 Configuration Method: DHCP ConfirmedInterfaceName: en0 Interface Name: en0 Network Signature: IPv4.Router=192.168.1.1;IPv4.RouterHardwareAddress=2c:b0:5d:25:af:69 Router: 192.168.1.1 Subnet Masks: 255.255.255.0 IPv6: Configuration Method: Automatic DNS: Server Addresses: 192.168.1.1 DHCP Server Responses: Domain Name Servers: 192.168.1.1 Lease Duration (seconds): 0 DHCP Message Type: 0x05 Routers: 192.168.1.1 Server Identifier: 192.168.1.1 Subnet Mask: 255.255.255.0 Ethernet: MAC Address: 28:cf:e9:17:e0:d1 Media Options: Media Subtype: Auto Select Proxies: Exceptions List: *.local, 169.254/16 FTP Passive Mode: Yes Service Order: 2 USB Ethernet: Type: Ethernet Hardware: Ethernet BSD Device Name: en3 IPv4: Configuration Method: DHCP IPv6: Configuration Method: Automatic Proxies: Exceptions List: *.local, 169.254/16 FTP Passive Mode: Yes Service Order: 3 AX88179 USB 3.0 to Gigabit Ethernet: Type: Ethernet Hardware: Ethernet BSD Device Name: en7 IPv4: Configuration Method: DHCP IPv6: Configuration Method: Automatic Proxies: Exceptions List: *.local, 169.254/16 FTP Passive Mode: Yes Service Order: 4 Thunderbolt FireWire: Type: FireWire Hardware: FireWire BSD Device Name: fw0 IPv4: Configuration Method: DHCP IPv6: Configuration Method: Automatic Ethernet: MAC Address: 00:50:b6:10:00:00:5d:31 Media Options: Full Duplex Media Subtype: Auto Select Proxies: Exceptions List: *.local, 169.254/16 FTP Passive Mode: Yes Service Order: 5 iPhone: Type: Ethernet Hardware: Ethernet BSD Device Name: en5 IPv4: Configuration Method: DHCP IPv6: Configuration Method: Automatic Proxies: Exceptions List: *.local, 169.254/16 FTP Passive Mode: Yes Service Order: 6 Bluetooth PAN: Type: Ethernet Hardware: Ethernet BSD Device Name: en4 IPv4: Configuration Method: DHCP IPv6: Configuration Method: Automatic Proxies: Exceptions List: *.local, 169.254/16 FTP Passive Mode: Yes Service Order: 7 '; preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0); // Print the entire match result var_dump($matches);

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 PHP, please visit: http://php.net/manual/en/ref.pcre.php