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

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"\d\d.-?.\w.-?.\d\d" test_str = ("root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen-533 Berkshire 5 min 27-Jun-18 14:27 9.0 --- --- 0.08\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen-533 Berkshire 27-Jun-18 9.0 18.9 7.7 ---\n" "root - ERROR - : 06/05/2019, 14:29:55 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:55 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen 247 Brock Street North 27-Jun-18 13.0 19 7.6 ---\n" "root - ERROR - : 06/05/2019, 14:29:55 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen 245 Brock St N 5min 09-Jul-18 14:35 9.0 --- --- 12.1\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen 245 Brock St N 09-Jul-18 9.0 7.5 22.4 ---\n" "root - ERROR - : 06/05/2019, 14:29:55 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen-665 Cherry Dr 5min 21-Jun-18 11:04 8.0 --- 3.93\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen-665 Cherry Dr 21-Jun-18 8.0 7.7 ---\n" "root - ERROR - : 06/05/2019, 14:29:55 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen 1390 Christina Street North 10-Oct-18 16.0 7.6 ---\n" "root - ERROR - : 06/05/2019, 14:29:55 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:55 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen 140 Conrad St 09-Jul-18 9.0 7.6 21.6 ---\n" "root - ERROR - : 06/05/2019, 14:29:55 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:55 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen 763 Copland 27-Jun-18 13.0 19.3 7.6 ---\n" "root - ERROR - : 06/05/2019, 14:29:55 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen-560 Cromwell St 5th 09-May-18 13:37 12.0 --- 16.1\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen-560 Cromwell St 09-May-18 12.0 7.0 ---\n" "root - ERROR - : 06/05/2019, 14:29:55 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:55 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:55 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen 534 Davis Street 5min 30-Aug-18 14:34 17.0 --- 9.05\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen 534 Davis Street 30-Aug-18 --- 7.5 ---\n" "root - ERROR - : 06/05/2019, 14:29:55 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen-534 Devine St 5min 25-Jun-18 08:04 10.0 --- --- 0.05\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen-534 Devine St 25-Jun-18 10.0 6.6 18.4 ---\n" "root - ERROR - : 06/05/2019, 14:29:55 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen 845 Devine St-5 Min 26-Jun-18 12:43 8.0 --- --- 75 0.17\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen 845 Devine St 26-Jun-18 8.0 18.5 7.6 --- ---\n" "root - ERROR - : 06/05/2019, 14:29:55 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen 357 East St-5 26-Sep-18 09:04 19.0 --- 0.09\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen 357 East St 19.0 7.8 ---\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen 357 East St-1 26-Sep-18 09:34 19.0 --- 0.27\n" "root - ERROR - : 06/05/2019, 14:29:55 : NR Kitchen 357 East St-2 26-Sep-18 09:34 19.0 --- 0.14\n" "root - ERROR - : 06/05/2019, 14:29:55 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen 306 Elgin St 10-Oct-18 16.0 8.0 ---\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen 308 Elgin St-1 26-Sep-18 11:32 19.0 --- 6.47\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen-641 Elm Ave 09-May-18 12.0 6.7 ---\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen-267 Emma St 29-May-18 11.0 7.8 ---\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen-272 Emma St 24-May-18 11.0 7.3 ---\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen-278 Emma St 29-May-18 11.0 7.4 ---\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen-297 Emma St 24-May-18 11.0 7.3 ---\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen-301 Emma 24-May-18 11.0 7.2 ---\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen-248 Emma St 08-May-18 16.0 7.4 ---\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen-257 Emma St 5th 08-May-18 12:30 16.0 --- 1.21\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen-263 Emma St 07-May-18 16.0 7.8 ---\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen-273 Emma St 07-May-18 12:41 16.0 7.7 ---\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen-277 Emma St 08-May-18 16.0 7.8 ---\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen-281 Emma St 07-May-18 13:48 16.0 7.9 ---\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen-284 Emma St 08-May-18 16.0 7.8 ---\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen-291 Emma St 07-May-18 14:30 16.0 7.8 ---\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen-299 Emma St 08-May-18 16.0 7.6 ---\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen-300 Emma St 08-May-18 16.0 7.9 ---\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen-247 Emma St 09-May-18 12.0 7.9 ---\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen-288 Emma St 10-May-18 12.0 7.8 ---\n" "root - ERROR - : 06/05/2019, 14:29:56 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:56 : NR Kitchen-303 Emma St 10-May-18 12.0 7.6 ---\n" "root - ERROR - : 06/05/2019, 14:29:57 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:57 : NR Kitchen 320 Exmouth 12-Jul-18 8.0 21.9 7.6 ---\n" "root - ERROR - : 06/05/2019, 14:29:57 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:57 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:57 : NR Kitchen 571 Flamingo DR 5 03-Oct-18 09:16 Curtis Salisbury 16.0 --- 0.07\n" "root - ERROR - : 06/05/2019, 14:29:57 : NR Kitchen 571 Flamingo DR 03-Oct-18 Curtis Salisbury 16.0 7.9 ---\n" "root - ERROR - : 06/05/2019, 14:29:57 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:57 : NR Kitchen 590 Flamingo 5min 14-Aug-18 15:04 12.0 --- --- 0.08\n" "root - ERROR - : 06/05/2019, 14:29:57 : NR Kitchen 590 Flamingo 14-Aug-18 12.0 7.6 22.3 ---\n" "root - ERROR - : 06/05/2019, 14:29:57 : -NR\n" "root - ERROR - : 06/05/2019, 14:29:57 : NR Kitchen 122 Forsyth North 31-Jul-18 Curtis Salisbury 12.0 21.6 7.4 ---\n" "root - ERROR - : 06/05/2019, 14:29:57 : -NR\n") matches = re.finditer(regex, test_str, re.MULTILINE) for matchNum, match in enumerate(matches, start=1): print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group())) for groupNum in range(0, len(match.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum))) # 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