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

import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String[] args) { final String regex = "^ ([\\w-_ ]+):\\n\\n +Type: AirPort\\n +Hardware.+\\n +BSD Device Name: (.+)\\n +IPv4 Addresses: (192[\\.\\d]+)$"; final String string = "ETHERNET DISCONNECTED, WI-FI RUNNING\n" + "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n\n\n" + "Network:\n\n" + " Thunderbolt Ethernet Slot 1:\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" + " Ethernet:\n" + " MAC Address: 00:50:b6:ca:f0:dc\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: 0\n\n" + " Thunderbolt Ethernet:\n\n" + " Type: Ethernet\n" + " Hardware: Ethernet\n" + " BSD Device Name: en6\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" + " Wi-Fi:\n\n" + " Type: AirPort\n" + " Hardware: AirPort\n" + " BSD Device Name: en0\n" + " IPv4 Addresses: 192.168.1.16\n" + " IPv4:\n" + " AdditionalRoutes:\n" + " DestinationAddress: 192.168.1.16\n" + " SubnetMask: 255.255.255.255\n" + " DestinationAddress: 169.254.0.0\n" + " SubnetMask: 255.255.0.0\n" + " Addresses: 192.168.1.16\n" + " ARPResolvedHardwareAddress: 2c:b0:5d:25:af:69\n" + " ARPResolvedIPAddress: 192.168.1.1\n" + " Configuration Method: DHCP\n" + " ConfirmedInterfaceName: en0\n" + " Interface Name: en0\n" + " Network Signature: IPv4.Router=192.168.1.1;IPv4.RouterHardwareAddress=2c:b0:5d:25:af:69\n" + " Router: 192.168.1.1\n" + " Subnet Masks: 255.255.255.0\n" + " IPv6:\n" + " Configuration Method: Automatic\n" + " DNS:\n" + " Server Addresses: 192.168.1.1\n" + " DHCP Server Responses:\n" + " Domain Name Servers: 192.168.1.1\n" + " Lease Duration (seconds): 0\n" + " DHCP Message Type: 0x05\n" + " Routers: 192.168.1.1\n" + " Server Identifier: 192.168.1.1\n" + " Subnet Mask: 255.255.255.0\n" + " Ethernet:\n" + " MAC Address: 28:cf:e9:17:e0:d1\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: 2\n\n" + " USB Ethernet:\n\n" + " Type: Ethernet\n" + " Hardware: Ethernet\n" + " BSD Device Name: en3\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" + " AX88179 USB 3.0 to Gigabit Ethernet:\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: 4\n\n" + " Thunderbolt 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:50:b6:10:00:00:5d:31\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: 5\n\n" + " iPhone:\n\n" + " Type: Ethernet\n" + " Hardware: Ethernet\n" + " BSD Device Name: en5\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: 6\n\n" + " Bluetooth PAN:\n\n" + " Type: Ethernet\n" + " Hardware: Ethernet\n" + " BSD Device Name: en4\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: 7\n"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); final Matcher matcher = pattern.matcher(string); while (matcher.find()) { System.out.println("Full match: " + matcher.group(0)); for (int i = 1; i <= matcher.groupCount(); i++) { System.out.println("Group " + i + ": " + matcher.group(i)); } } } }

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 Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html