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 = "(?<wholekey>(?s)^\\[.*?\\].*?(?=^\\[|\\z))"; final String string = "Windows Registry Editor Version 5.00\n\n" + "[HKEY_LOCAL_MACHINE]\n" + "\"ServiceLastKnownStatus\"=dword:00000002\n\n" + "[HKEY_LOCAL_MACHINE\\BCD00000000]\n\n" + "[HKEY_CURRENT_USER\\AppEvents\\Schemes\\Apps\\.Default\\Notification.Default\\.Current]\n" + "@=\"C:\\\\Windows\\\\media\\\\Windows Notify System Generic.wav\"\n\n" + "[HKEY_LOCAL_MACHINE\\BCD00000000\\Description]\n" + "\"KeyName\"=\"BCD00000000\"\n" + "\"TreatAsSystem\"=dword:00000001\n" + "\"GuidCache\"=hex:f4,60,9c,00,7b,c3,da,01,03,27,00,00,8a,d6,94,57,42,4c,7f,29,8a,\\\n" + " 00,00,00\n" + "\"Element\"=hex:00\n" + "\"Element\"=hex(7):7b,00,37,00,65,00,61,00,32,00,65,00,31,00,61,00,63,00,2d,00,\\\n" + " 32,00,65,00,36,00,31,00,2d,00,34,00,37,00,32,00,38,00,2d,00,61,00,61,00,61,\\\n" + " 00,33,00,2d,00,38,00,39,00,36,00,64,00,39,00,64,00,30,00,61,00,39,00,66,00,\\\n" + " 30,00,65,00,7d,00,00,00,00,00\n" + "\"Type\"=dword:101fffff\n" + "\"Element\"=\"EFI PXE 0 for IPv4 (74-5D-22-21-B1-95) \"\n" + "\"OSMaxVersionTested\"=hex(b):00,00,ff,3f,00,00,0a,00\n" + "\"OSMinVersion\"=hex(b):00,00,00,00,00,00,0a,00\n" + "\"WMP11.AssocFile.3G2\"=hex(0):\n" + "\"Drivers\"=hex(100000):\n" + "\"DriverType\"=hex(200000):\n" + "\"lParam\"=hex(4):00,00,00,00,00,00,00,00\n\n" + "[HKEY_LOCAL_MACHINE\\BCD00000000\\Objects\\{3edef9e6-2f6e-11ef-968a-9457424c7f29}\\Elements]\n\n" + "[HKEY_LOCAL_MACHINE\\BCD00000000\\Objects\\{3edef9e9-2f6e-11ef-968a-9457424c7f29}]\n" + "\"command\"=hex(2):22,00,25,00,70,00,72,00,6f,00,67,00,72,00,61,00,6d,00,46,00,\\\n" + " 69,00,6c,00,65,00,73,00,25,00,5c,00,57,00,69,00,6e,00,64,00,6f,00,77,00,73,\\\n" + " 00,20,00,4d,00,61,00,69,00,6c,00,5c,00,57,00,61,00,62,00,2e,00,65,00,78,00,\\\n" + " 65,00,22,00,20,00,2f,00,43,00,72,00,65,00,61,00,74,00,65,00,43,00,6f,00,6e,\\\n" + " 00,74,00,61,00,63,00,74,00,20,00,22,00,25,00,31,00,22,00,00,00\n" + "\"PrimaryLineOutTopo\"=hex(40007):41,00,70,00,6f,00,50,00,72,00,65,00,73,00,65,\\\n" + " 00,74,00,31,00,00,00,00,00\n" + "\"Element\"=hex(7):7b,00,31,00,61,00,66,00,61,00,39,00,63,00,34,00,39,00,2d,00,\\\n" + " 31,00,36,00,61,00,62,00,2d,00,34,00,61,00,35,00,63,00,2d,00,39,00,30,00,31,\\\n" + " 00,62,00,2d,00,32,00,31,00,32,00,38,00,30,00,32,00,64,00,61,00,39,00,34,00,\\\n" + " 36,00,30,00,7d,00,00,00,00,00\n" + "\"Isa\"=hex(8):01,00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,28,00,00,00,01,01,\\\n" + " 00,00,00,00,00,00,00,00,00,00,00,01,00,00,01,03,00,00,e8,42,00,00,00,00,00,\\\n" + " 00,08,00,00,00,01,03,00,00,e8,4a,00,00,00,00,00,00,08,00,00,00,01,03,00,00,\\\n" + " e8,82,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,86,00,00,00,00,00,00,08,\\\n" + " 00,00,00,01,03,00,00,e8,8a,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,8e,\\\n" + " 00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,92,00,00,00,00,00,00,08,00,00,\\\n" + " 00,01,03,00,00,e8,96,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,9a,00,00,\\\n" + " 00,00,00,00,08,00,00,00,01,03,00,00,e8,9e,00,00,00,00,00,00,08,00,00,00,01,\\\n" + " 03,00,00,e8,a2,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,a6,00,00,00,00,\\\n" + " 00,00,08,00,00,00,01,03,00,00,e8,aa,00,00,00,00,00,00,08,00,00,00,01,03,00,\\\n" + " 00,e8,ae,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,b6,00,00,00,00,00,00,\\\n" + " 08,00,00,00,01,03,00,00,e8,ba,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,\\\n" + " be,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,c2,00,00,00,00,00,00,08,00,\\\n" + " 00,00,01,03,00,00,e8,c6,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,ca,00,\\\n" + " 00,00,00,00,00,08,00,00,00,01,03,00,00,e8,ce,00,00,00,00,00,00,08,00,00,00,\\\n" + " 01,03,00,00,e8,d2,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,d6,00,00,00,\\\n" + " 00,00,00,08,00,00,00,01,03,00,00,e8,da,00,00,00,00,00,00,08,00,00,00,01,03,\\\n" + " 00,00,e8,de,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,e2,00,00,00,00,00,\\\n" + " 00,08,00,00,00,01,03,00,00,e8,e6,00,00,00,00,00,00,08,00,00,00,01,03,00,00,\\\n" + " e8,ea,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,ee,00,00,00,00,00,00,08,\\\n" + " 00,00,00,01,03,00,00,ee,f6,00,00,00,00,00,00,02,00,00,00,01,03,00,00,ee,fa,\\\n" + " 00,00,00,00,00,00,02,00,00,00,01,03,00,00,ee,fe,00,00,00,00,00,00,02,00,00,\\\n" + " 00,02,03,00,00,03,00,00,00,03,00,00,00,ff,ff,ff,ff,02,03,00,00,04,00,00,00,\\\n" + " 04,00,00,00,ff,ff,ff,ff,02,03,00,00,0e,00,00,00,0e,00,00,00,ff,ff,ff,ff,02,\\\n" + " 03,00,00,06,00,00,00,06,00,00,00,ff,ff,ff,ff,02,03,00,00,0c,00,00,00,0c,00,\\\n" + " 00,00,ff,ff,ff,ff,02,03,00,00,01,00,00,00,01,00,00,00,ff,ff,ff,ff,03,03,00,\\\n" + " 00,ff,ff,bf,ff,00,00,00,00,00,00,40,00\n" + "\"Configuration Data\"=hex(9):ff,ff,ff,ff,ff,ff,ff,ff,00,00,00,00,02,00,00,00,05,\\\n" + " 00,00,00,0c,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,ff,ff,ff,ff,\\\n" + " 00,00,ff,ff,01,00,05,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\\\n" + " 00\n" + "\"Pci\"=hex(a):e8,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\\\n" + " 00,00,00,00,00,00,01,00,00,00,01,00,01,00,0e,00,00,00,00,01,00,00,00,00,00,\\\n" + " 00,00,00,00,00,00,00,00,00,00,00,01,00,00,00,00,00,ff,ff,ff,ff,00,00,00,00,\\\n" + " 08,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,05,00,00,00,00,00,00,ff,\\\n" + " ff,00,00,00,00,00,00,08,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,40,01,\\\n" + " 00,00,00,00,00,00,7f,01,00,00,00,00,00,00,08,01,00,00,00,00,00,00,00,00,00,\\\n" + " 00,00,00,00,00,00,02,00,00,00,00,00,00,ff,02,00,00,00,00,00,00,08,01,00,00,\\\n" + " 00,00,00,00,00,00,00,00,00,00,00,00,00,03,00,00,00,00,00,00,6f,03,00,00,00,\\\n" + " 00,00,00,08,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,78,03,00,00,00,00,\\\n" + " 00,00,7a,03,00,00,00,00,00,00,08,01,00,00,00,00,00,00,00,00,00,00,00,00,00,\\\n" + " 00,e8,02,00,00,00,00,00,00,ff,02,00,00,00,00,00,00,08,01,00,00,00,00,00,00,\\\n" + " 00,00,00,00,00,00,00,00,f0,01,00,00,00,00,00,00,f8,01,00,00,00,00,00,00,08,\\\n" + " 01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,b0,03,00,00,00,00,00,00,cf,03,\\\n" + " 00,00,00,00,00,00,08,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,e8,03,00,\\\n" + " 00,00,00,00,00,ff,03,00,00,00,00,00,00,08,01,00,00,00,00,00,00,00,00,00,00,\\\n" + " 00,00,00,00,ce,01,00,00,00,00,00,00,cf,01,00,00,00,00,00,00,08,01,00,00,00,\\\n" + " 00,00,00,00,00,00,00,00,00,00,00,00,01,00,00,00,00,00,00,ff,03,00,00,00,00,\\\n" + " 00,00,00,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,10,00,00,00,00,\\\n" + " 00,ff,ff,ff,ff,ff,ff,ff,ff,08,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\\\n" + " 00,00,08,00,00,00,00,00,ff,ff,0d,00,00,00,00,00"; 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