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

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"^(\w+) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})\@\d+ ((?:myself,)?[\w?]+) ([\w-]+) ([\d]+) ([\d]+) ([\d]+) ([\w]+) ?([ \-\d]+)? ?(\[[\d]+-(?:>|<)-[\w]+\])?" test_str = ("6535e1a5ba67e01527275299ba4328f26872e3d0 10.229.170.172:7000 slave 81bb21169500bf77638103c9c09583101e475639 0 1597310146695 7 connected\n" "d2b0b248493fcfbf8522e9546f1057706a18187a 10.229.170.171:7000 slave 2236a749ebf3a8bce1f6ac1ffc3100b28db46604 0 1597310148732 11 connected\n" "db50d92a23cee951eff8c5d9f64888a9d1a00a13 10.230.140.208:7000 slave f9d94b7ee9cf684199a1b713ae3f37a1de820f52 0 1597310149249 8 connected\n" "ce7410975901a74c1475c449fd8216cd22168865 10.230.140.208:7001 myself,master - 0 0 10 connected 13505-16383 [13505->-2236a749ebf3a8bce1f6ac1ffc3100b28db46604]\n" "bf6f4d7f5330fe134a575bcf7e36fd012b4a5cf4 10.230.140.209:7001 master - 0 1597310145681 5 connected 3277-6553\n" "518f3da0ab884b2808e278e7d885d77fa7b70abc 10.230.140.210:7000 slave bf6f4d7f5330fe134a575bcf7e36fd012b4a5cf4 0 1597310143665 5 connected\n" "f9d94b7ee9cf684199a1b713ae3f37a1de820f52 10.230.140.209:7000 master - 0 1597310144673 8 connected 50-3276\n" "d2037c152950b5927795d739d1f60d9230bc5243 10.229.170.172:7001 slave ce7410975901a74c1475c449fd8216cd22168865 0 1597310148608 10 connected\n\n\n" "2236a749ebf3a8bce1f6ac1ffc3100b28db46604 10.230.140.210:7001 master - 0 1597310141662 11 connected 0-49 6554-9880 13108-13220\n" "81bb21169500bf77638103c9c09583101e475639 10.229.170.171:7001 master - 0 1597310139156 6 connected 9881-13107\n" "6535e1a5ba67e01527275299ba4328f26872e3d0 10.229.170.172:7000 slave 81bb21169500bf77638103c9c09583101e475639 0 1597310142666 7 connected\n" "d2b0b248493fcfbf8522e9546f1057706a18187a 10.229.170.171:7000 slave 2236a749ebf3a8bce1f6ac1ffc3100b28db46604 0 1597310143164 11 connected\n" "db50d92a23cee951eff8c5d9f64888a9d1a00a13 10.230.140.208:7000 slave f9d94b7ee9cf684199a1b713ae3f37a1de820f52 0 1597310141662 8 connected\n" "ce7410975901a74c1475c449fd8216cd22168865 10.230.140.208:7001 myself,master - 0 0 10 connected 13221-16383 [13221->-2236a749ebf3a8bce1f6ac1ffc3100b28db46604]\n" "bf6f4d7f5330fe134a575bcf7e36fd012b4a5cf4 10.230.140.209:7001 master - 0 1597310140659 5 connected 3277-6553\n" "518f3da0ab884b2808e278e7d885d77fa7b70abc 10.230.140.210:7000 slave bf6f4d7f5330fe134a575bcf7e36fd012b4a5cf4 0 1597310143665 5 connected\n" "f9d94b7ee9cf684199a1b713ae3f37a1de820f52 10.230.140.209:7000 master - 0 1597310144673 8 connected 50-3276\n" "d2037c152950b5927795d739d1f60d9230bc5243 10.229.170.172:7001 slave ce7410975901a74c1475c449fd8216cd22168865 1597310144167 1597310136651 10 connected\n\n" "ce7410975901a74c1475c449fd8216cd22168865 10.230.140.208:7001 myself,master - 0 0 12 connected 0-49 6554-6842 15254-16383 [6843-<-2236a749ebf3a8bce1f6ac1ffc3100b28db46604]\n" "bf6f4d7f5330fe134a575bcf7e36fd012b4a5cf4 10.230.140.209:7001 master - 0 1597319746589 5 connected 3277-6553\n" "518f3da0ab884b2808e278e7d885d77fa7b70abc 10.230.140.210:7000 slave bf6f4d7f5330fe134a575bcf7e36fd012b4a5cf4 0 1597319749595 5 connected\n" "f9d94b7ee9cf684199a1b713ae3f37a1de820f52 10.230.140.209:7000 master - 0 1597319746089 8 connected 50-3276\n" "d2037c152950b5927795d739d1f60d9230bc5243 10.229.170.172:7001 slave ce7410975901a74c1475c449fd8216cd22168865 0 1597319747590 12 connected\n\n" "2114d318b6bff84367748b0af1480414625db481 10.225.129.237:7000@17000 slave 17a137f8c8ab3886abf9ef756a9e46ce00b3db46 0 1597323334000 8 connected\n" "f7b8bc2f99e217a9aa33165771d63ac00130bf06 10.225.129.236:7000@17000 slave 29accc30f88be4c5a1b5c316ef4a07db11e773ca 0 1597323335000 1 connected\n" "29accc30f88be4c5a1b5c316ef4a07db11e773ca 10.225.129.235:7000@17000 myself,master - 0 1597323336000 1 connected 0-2730\n" "a582a8683f6ba747ec8b1ae959c2aba2009c6fec 10.225.129.235:7001@17001 slave 9445f535348daf42827d0610b42a2f6f67c8c672 0 1597323337133 7 connected\n" "9445f535348daf42827d0610b42a2f6f67c8c672 10.225.129.240:7001@17001 master - 0 1597323336533 7 connected 13655-16383") 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