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
No Match

r"
"
gm

Test String

Substitution

Processing...

Code Generator

Generated Code

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"([0-9A-F]{2}:){5}[0-9A-F]{2}" test_str = ("[DHCP IP: (192.168.1.17)] to MAC address 00:50:8D:91:40:A3, Monday, Aug 10,2020 21:06:09\n" "[DHCP IP: (192.168.1.11)] to MAC address 00:FA:21:46:F0:70, Monday, Aug 10,2020 19:41:28\n" "[DHCP IP: (192.168.1.14)] to MAC address 8C:79:67:C1:C4:F7, Monday, Aug 10,2020 19:41:12\n" "[DHCP IP: (192.168.1.7)] to MAC address 00:1C:C0:F0:8A:94, Monday, Aug 10,2020 16:51:41\n" "[DHCP IP: (192.168.1.17)] to MAC address 00:50:8D:91:40:A3, Monday, Aug 10,2020 15:53:03\n" "[DHCP IP: (192.168.1.12)] to MAC address 0C:62:A6:E4:60:A6, Monday, Aug 10,2020 15:48:05\n" "[Dynamic DNS] host name 520techsec.hopto.org registration successful, Monday, Aug 10,2020 14:29:55\n" "[Dynamic DNS] host name 520techsec.hopto.org registration failure, Monday, Aug 10,2020 14:29:24\n" "[Dynamic DNS] host name 520techsec.hopto.org registration failure, Monday, Aug 10,2020 14:28:53\n" "[Dynamic DNS] host name 520techsec.hopto.org registration failure, Monday, Aug 10,2020 14:28:22\n" "[DHCP IP: (192.168.1.10)] to MAC address 6C:C2:17:1E:B4:B9, Monday, Aug 10,2020 09:51:03\n" "[DHCP IP: (192.168.1.17)] to MAC address 00:50:8D:91:40:A3, Monday, Aug 10,2020 09:04:50\n" "[DHCP IP: (192.168.1.11)] to MAC address 00:FA:21:46:F0:70, Monday, Aug 10,2020 08:10:16\n" "[DHCP IP: (192.168.1.14)] to MAC address 8C:79:67:C1:C4:F7, Monday, Aug 10,2020 08:10:01\n" "[DHCP IP: (192.168.1.13)] to MAC address 00:16:44:DF:C7:92, Monday, Aug 10,2020 07:11:22\n" "[DHCP IP: (192.168.1.13)] to MAC address 00:16:44:DF:C7:92, Monday, Aug 10,2020 07:04:23\n" "[DHCP IP: (192.168.1.7)] to MAC address 00:1C:C0:F0:8A:94, Monday, Aug 10,2020 04:51:42\n" "[DHCP IP: (192.168.1.17)] to MAC address 00:50:8D:91:40:A3, Monday, Aug 10,2020 03:52:41\n" "[DHCP IP: (192.168.1.12)] to MAC address 0C:62:A6:E4:60:A6, Monday, Aug 10,2020 03:48:20\n" "[DHCP IP: (192.168.1.10)] to MAC address 6C:C2:17:1E:B4:B9, Sunday, Aug 09,2020 21:15:05\n" "[DHCP IP: (192.168.1.17)] to MAC address 00:50:8D:91:40:A3, Sunday, Aug 09,2020 21:06:42\n" "[DHCP IP: (192.168.1.8)] to MAC address 9C:B6:D0:73:52:AB, Sunday, Aug 09,2020 20:53:45\n" "[DHCP IP: (192.168.1.11)] to MAC address 00:FA:21:46:F0:70, Sunday, Aug 09,2020 20:39:03\n" "[DHCP IP: (192.168.1.14)] to MAC address 8C:79:67:C1:C4:F7, Sunday, Aug 09,2020 20:38:48\n" "[DHCP IP: (192.168.1.8)] to MAC address 9C:B6:D0:73:52:AB, Sunday, Aug 09,2020 20:33:15\n" "[DHCP IP: (192.168.1.7)] to MAC address 00:1C:C0:F0:8A:94, Sunday, Aug 09,2020 16:51:41\n" "[DHCP IP: (192.168.1.17)] to MAC address 00:50:8D:91:40:A3, Sunday, Aug 09,2020 15:53:06\n" "[DHCP IP: (192.168.1.12)] to MAC address 0C:62:A6:E4:60:A6, Sunday, Aug 09,2020 15:48:33\n" "[Dynamic DNS] host name 520techsec.hopto.org registration successful, Sunday, Aug 09,2020 14:28:12\n" "[Dynamic DNS] host name 520techsec.hopto.org registration failure, Sunday, Aug 09,2020 14:27:41\n" "[Dynamic DNS] host name 520techsec.hopto.org registration failure, Sunday, Aug 09,2020 14:27:10\n" "[Dynamic DNS] host name 520techsec.hopto.org registration failure, Sunday, Aug 09,2020 14:26:38\n" "[DHCP IP: (192.168.1.11)] to MAC address 00:FA:21:46:F0:70, Sunday, Aug 09,2020 09:07:51\n" "[DHCP IP: (192.168.1.14)] to MAC address 8C:79:67:C1:C4:F7, Sunday, Aug 09,2020 09:07:37\n" "[DHCP IP: (192.168.1.17)] to MAC address 00:50:8D:91:40:A3, Sunday, Aug 09,2020 09:05:37") subst = "" # You can manually specify the number of replacements by changing the 4th argument result = re.sub(regex, subst, test_str, 0, re.MULTILINE) if result: print (result) # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

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