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

#include <StringConstants.au3> ; to declare the Constants of StringRegExp #include <Array.au3> ; UDF needed for _ArrayDisplay and _ArrayConcatenate Local $sRegex = "(?m)^ ([\w-_ ]+):\n\n +Type: Ethernet\n +Hardware.+\n +BSD Device Name: (.+)\n +IPv4 Addresses: (192[\.\d]+)$" Local $sString = "ETHERNET CONNECTED, NO WI-FI" & @CRLF & _ "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" & @CRLF & _ "Below Source Text from Mac Shell Script:" & @CRLF & _ "system_profiler SPNetworkDataType" & @CRLF & _ "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" & @CRLF & _ "Network:" & @CRLF & _ "" & @CRLF & _ " Thunderbolt Ethernet Slot 1:" & @CRLF & _ "" & @CRLF & _ " Type: Ethernet" & @CRLF & _ " Hardware: Ethernet" & @CRLF & _ " BSD Device Name: en8" & @CRLF & _ " IPv4 Addresses: 192.168.1.15" & @CRLF & _ " IPv4:" & @CRLF & _ " AdditionalRoutes:" & @CRLF & _ " DestinationAddress: 192.168.1.15" & @CRLF & _ " SubnetMask: 255.255.255.255" & @CRLF & _ " DestinationAddress: 169.254.0.0" & @CRLF & _ " SubnetMask: 255.255.0.0" & @CRLF & _ " Addresses: 192.168.1.15" & @CRLF & _ " ARPResolvedHardwareAddress: 2c:b0:5d:25:af:69" & @CRLF & _ " ARPResolvedIPAddress: 192.168.1.1" & @CRLF & _ " Configuration Method: DHCP" & @CRLF & _ " ConfirmedInterfaceName: en8" & @CRLF & _ " Interface Name: en8" & @CRLF & _ " Network Signature: IPv4.Router=192.168.1.1;IPv4.RouterHardwareAddress=2c:b0:5d:25:af:69" & @CRLF & _ " Router: 192.168.1.1" & @CRLF & _ " Subnet Masks: 255.255.255.0" & @CRLF & _ " IPv6:" & @CRLF & _ " Configuration Method: Automatic" & @CRLF & _ " DNS:" & @CRLF & _ " Server Addresses: 192.168.1.1" & @CRLF & _ " DHCP Server Responses:" & @CRLF & _ " Domain Name Servers: 192.168.1.1" & @CRLF & _ " Lease Duration (seconds): 0" & @CRLF & _ " DHCP Message Type: 0x05" & @CRLF & _ " Routers: 192.168.1.1" & @CRLF & _ " Server Identifier: 192.168.1.1" & @CRLF & _ " Subnet Mask: 255.255.255.0" & @CRLF & _ " Ethernet:" & @CRLF & _ " MAC Address: 00:50:b6:ca:f0:dc" & @CRLF & _ " Media Options: Full Duplex, Flow Control" & @CRLF & _ " Media Subtype: 1000baseT" & @CRLF & _ " Proxies:" & @CRLF & _ " Exceptions List: *.local, 169.254/16" & @CRLF & _ " FTP Passive Mode: Yes" & @CRLF & _ " Sleep Proxies:" & @CRLF & _ " 1 Family Room Apple TV:" & @CRLF & _ " Marginal Power: 60" & @CRLF & _ " Metric: 703560" & @CRLF & _ " Portability: 35" & @CRLF & _ " Total Power: 63" & @CRLF & _ " Type: 70" & @CRLF & _ " Service Order: 0" & @CRLF & _ "" & @CRLF & _ " Wi-Fi:" & @CRLF & _ "" & @CRLF & _ " Type: AirPort" & @CRLF & _ " Hardware: AirPort" & @CRLF & _ " BSD Device Name: en0" & @CRLF & _ " IPv4 Addresses: 192.168.1.16" & @CRLF & _ " IPv4:" & @CRLF & _ " AdditionalRoutes:" & @CRLF & _ " DestinationAddress: 192.168.1.16" & @CRLF & _ " SubnetMask: 255.255.255.255" & @CRLF & _ " DestinationAddress: 169.254.0.0" & @CRLF & _ " SubnetMask: 255.255.0.0" & @CRLF & _ " Addresses: 192.168.1.16" & @CRLF & _ " ARPResolvedHardwareAddress: 2c:b0:5d:25:af:69" & @CRLF & _ " ARPResolvedIPAddress: 192.168.1.1" & @CRLF & _ " Configuration Method: DHCP" & @CRLF & _ " ConfirmedInterfaceName: en0" & @CRLF & _ " Interface Name: en0" & @CRLF & _ " Network Signature: IPv4.Router=192.168.1.1;IPv4.RouterHardwareAddress=2c:b0:5d:25:af:69" & @CRLF & _ " Router: 192.168.1.1" & @CRLF & _ " Subnet Masks: 255.255.255.0" & @CRLF & _ " IPv6:" & @CRLF & _ " Configuration Method: Automatic" & @CRLF & _ " DNS:" & @CRLF & _ " Server Addresses: 192.168.1.1" & @CRLF & _ " DHCP Server Responses:" & @CRLF & _ " Domain Name Servers: 192.168.1.1" & @CRLF & _ " Lease Duration (seconds): 0" & @CRLF & _ " DHCP Message Type: 0x05" & @CRLF & _ " Routers: 192.168.1.1" & @CRLF & _ " Server Identifier: 192.168.1.1" & @CRLF & _ " Subnet Mask: 255.255.255.0" & @CRLF & _ " Ethernet:" & @CRLF & _ " MAC Address: 28:cf:e9:17:e0:d1" & @CRLF & _ " Media Options: " & @CRLF & _ " Media Subtype: Auto Select" & @CRLF & _ " Proxies:" & @CRLF & _ " Exceptions List: *.local, 169.254/16" & @CRLF & _ " FTP Passive Mode: Yes" & @CRLF & _ " Service Order: 2" & @CRLF & _ "" & @CRLF & _ " Thunderbolt Ethernet:" & @CRLF & _ "" & @CRLF & _ " Type: Ethernet" & @CRLF & _ " Hardware: Ethernet" & @CRLF & _ " BSD Device Name: en6" & @CRLF & _ " IPv4:" & @CRLF & _ " Configuration Method: DHCP" & @CRLF & _ " IPv6:" & @CRLF & _ " Configuration Method: Automatic" & @CRLF & _ " Proxies:" & @CRLF & _ " Exceptions List: *.local, 169.254/16" & @CRLF & _ " FTP Passive Mode: Yes" & @CRLF & _ " Service Order: 1" & @CRLF & _ "" & @CRLF & _ " Wi-Fi:" & @CRLF & _ "" & @CRLF & _ " Type: AirPort" & @CRLF & _ " Hardware: AirPort" & @CRLF & _ " BSD Device Name: en0" & @CRLF & _ " IPv4:" & @CRLF & _ " Configuration Method: DHCP" & @CRLF & _ " IPv6:" & @CRLF & _ " Configuration Method: Automatic" & @CRLF & _ " Ethernet:" & @CRLF & _ " MAC Address: 28:cf:e9:17:e0:d1" & @CRLF & _ " Media Options: " & @CRLF & _ " Media Subtype: Auto Select" & @CRLF & _ " Proxies:" & @CRLF & _ " Exceptions List: *.local, 169.254/16" & @CRLF & _ " FTP Passive Mode: Yes" & @CRLF & _ " Service Order: 2" & @CRLF & _ "" & @CRLF & _ " USB Ethernet:" & @CRLF & _ "" & @CRLF & _ " Type: Ethernet" & @CRLF & _ " Hardware: Ethernet" & @CRLF & _ " BSD Device Name: en3" & @CRLF & _ " IPv4:" & @CRLF & _ " Configuration Method: DHCP" & @CRLF & _ " IPv6:" & @CRLF & _ " Configuration Method: Automatic" & @CRLF & _ " Proxies:" & @CRLF & _ " Exceptions List: *.local, 169.254/16" & @CRLF & _ " FTP Passive Mode: Yes" & @CRLF & _ " Service Order: 3" & @CRLF & _ "" & @CRLF & _ " AX88179 USB 3.0 to Gigabit Ethernet:" & @CRLF & _ "" & @CRLF & _ " Type: Ethernet" & @CRLF & _ " Hardware: Ethernet" & @CRLF & _ " BSD Device Name: en7" & @CRLF & _ " IPv4:" & @CRLF & _ " Configuration Method: DHCP" & @CRLF & _ " IPv6:" & @CRLF & _ " Configuration Method: Automatic" & @CRLF & _ " Proxies:" & @CRLF & _ " Exceptions List: *.local, 169.254/16" & @CRLF & _ " FTP Passive Mode: Yes" & @CRLF & _ " Service Order: 4" & @CRLF & _ "" & @CRLF & _ " Thunderbolt FireWire:" & @CRLF & _ "" & @CRLF & _ " Type: FireWire" & @CRLF & _ " Hardware: FireWire" & @CRLF & _ " BSD Device Name: fw0" & @CRLF & _ " IPv4:" & @CRLF & _ " Configuration Method: DHCP" & @CRLF & _ " IPv6:" & @CRLF & _ " Configuration Method: Automatic" & @CRLF & _ " Ethernet:" & @CRLF & _ " MAC Address: 00:50:b6:10:00:00:5d:31" & @CRLF & _ " Media Options: Full Duplex" & @CRLF & _ " Media Subtype: Auto Select" & @CRLF & _ " Proxies:" & @CRLF & _ " Exceptions List: *.local, 169.254/16" & @CRLF & _ " FTP Passive Mode: Yes" & @CRLF & _ " Service Order: 5" & @CRLF & _ "" & @CRLF & _ " iPhone:" & @CRLF & _ "" & @CRLF & _ " Type: Ethernet" & @CRLF & _ " Hardware: Ethernet" & @CRLF & _ " BSD Device Name: en5" & @CRLF & _ " IPv4:" & @CRLF & _ " Configuration Method: DHCP" & @CRLF & _ " IPv6:" & @CRLF & _ " Configuration Method: Automatic" & @CRLF & _ " Proxies:" & @CRLF & _ " Exceptions List: *.local, 169.254/16" & @CRLF & _ " FTP Passive Mode: Yes" & @CRLF & _ " Service Order: 6" & @CRLF & _ "" & @CRLF & _ " Bluetooth PAN:" & @CRLF & _ "" & @CRLF & _ " Type: Ethernet" & @CRLF & _ " Hardware: Ethernet" & @CRLF & _ " BSD Device Name: en4" & @CRLF & _ " IPv4:" & @CRLF & _ " Configuration Method: DHCP" & @CRLF & _ " IPv6:" & @CRLF & _ " Configuration Method: Automatic" & @CRLF & _ " Proxies:" & @CRLF & _ " Exceptions List: *.local, 169.254/16" & @CRLF & _ " FTP Passive Mode: Yes" & @CRLF & _ " Service Order: 7" & @CRLF & _ "" & @CRLF & _ "~~~~~~~~~~~~~~~~~~~~~~~~" & @CRLF & _ "Type: Ethernet\n +Hardware.+\n +BSD.+\n +IPv4 Addresses: 192[\.\d]+$" Local $aArray = StringRegExp($sString, $sRegex, $STR_REGEXPARRAYGLOBALFULLMATCH) Local $aFullArray[0] For $i = 0 To UBound($aArray) -1 _ArrayConcatenate($aFullArray, $aArray[$i]) Next $aArray = $aFullArray ; Present the entire match result _ArrayDisplay($aArray, "Result")

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 AutoIt, please visit: https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm