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
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • 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"([0-9]+)\s+([-.a-zA-Z0-9]+)\s+(\(([-.a-zA-Z0-9]+)\))?(.*)$" test_str = (" 1 10.110.255.129 42.759 ms 42.721 ms 42.718 ms\n" " 2 10.24.171.1 48.262 ms 48.628 ms 48.626 ms\n" " 3 10.110.255.186 43.377 ms 43.588 ms 43.602 ms\n" " 4 10.251.5.234 44.628 ms 44.625 ms 44.620 ms\n" " 5 10.251.5.162 163.039 ms 163.590 ms 163.581 ms\n" " 6 10.251.5.153 160.607 ms 157.800 ms 158.146 ms\n" " 7 10.3.63.132 181.292 ms 181.304 ms 181.171 ms\n" " 8 10.3.63.162 158.832 ms 162.948 ms 162.924 ms\n" " 9 10.3.64.239 156.895 ms 155.873 ms 156.368 ms\n\n" " 1 10.110.255.129 (10.110.255.129) 46.122 ms 46.160 ms 46.155 ms\n" " 2 jira1.cos.git.jdsu.net (10.24.171.1) 49.325 ms 49.308 ms 49.289 ms\n" " 3 10.110.255.186 (10.110.255.186) 47.280 ms 47.263 ms 47.245 ms\n" " 4 10.251.5.234 (10.251.5.234) 49.201 ms 49.184 ms 49.169 ms\n" " 5 10.251.5.162 (10.251.5.162) 181.687 ms 181.928 ms 181.910 ms\n" " 6 10.251.5.153 (10.251.5.153) 176.666 ms 169.844 ms 171.617 ms\n" " 7 10.3.63.132 (10.3.63.132) 172.793 ms 174.712 ms 174.690 ms\n" " 8 10.3.63.162 (10.3.63.162) 186.754 ms 187.607 ms 187.592 ms\n" " 9 10.3.64.239 (10.3.64.239) 173.878 ms 175.426 ms 175.500 ms\n\n\n" "magic scenarios: :D (be hostname)\n\n" " 1 91.121.19.253 1.349 ms 1.343 ms 1.862 ms\n" " 2 37.187.231.105 0.649 ms 0.602 ms 37.187.231.95 0.739 ms\n" " 3 37.187.232.88 12.179 ms 37.187.232.96 12.365 ms 12.424 ms\n" " 4 80.81.194.118 42.681 ms 42.771 ms 42.915 ms\n" " 5 213.252.224.238 43.622 ms 43.458 ms 43.479 ms\n" " 6 84.15.6.230 43.573 ms 84.15.6.226 43.323 ms 213.226.129.50 40.188 ms\n" " 7 141.136.40.34 40.250 ms 213.226.129.50 40.059 ms 40.051 ms\n\n\n" "magic scenarios: :D (su hostname)\n\n\n" " 1 rbx1-3b-a9.fr.eu (91.121.19.253) 1.648 ms 3.061 ms 3.220 ms\n" " 2 be11-315.rbx-g2-a9.fr.eu (37.187.231.107) 0.763 ms be11-315.rbx-g1-a9.fr.eu (37.187.231.105) 0.821 ms be11-146.rbx-g2-a9.fr.eu (37.187.231.97) 1.300 ms\n" " 3 be100-1120.fra-5-a9.de.eu (37.187.232.96) 12.250 ms be100-1105.fra-1-a9.de.eu (37.187.232.88) 12.354 ms be100-1120.fra-5-a9.de.eu (37.187.232.96) 12.293 ms\n" " 4 r2223.bee.lt (80.81.194.118) 42.643 ms 42.734 ms 42.870 ms\n" " 5 213.252.224.238 (213.252.224.238) 43.368 ms 43.399 ms 84.15.6.226 (84.15.6.226) 43.676 ms\n" " 6 84.15.6.226 (84.15.6.226) 43.460 ms 213.226.129.50 (213.226.129.50) 40.203 ms 84.15.6.226 (84.15.6.226) 43.329 ms\n" " 7 s1v3.e-lietuva.lt (141.136.40.34) 40.225 ms 213.226.129.50 (213.226.129.50) 40.106 ms 40.082 ms\n\n\n" " 1 rbx1-3b-a9.fr.eu (91.121.19.253) 1.648 ms 3.061 ms 3.220 ms\n\n" " 2 be11-315.rbx-g2-a9.fr.eu (37.187.231.107) 0.763 ms \n" " 2 be11-315.rbx-g1-a9.fr.eu (37.187.231.105) 0.821 ms \n" " 2 be11-146.rbx-g2-a9.fr.eu (37.187.231.97) 1.300 ms\n" " \n" " 3 be100-1120.fra-5-a9.de.eu (37.187.232.96) 12.250 ms \n" " 3 be100-1105.fra-1-a9.de.eu (37.187.232.88) 12.354 ms \n" " 3 be100-1120.fra-5-a9.de.eu (37.187.232.96) 12.293 ms\n\n" " 4 r2223.bee.lt (80.81.194.118) 42.643 ms 42.734 ms 42.870 ms\n\n" " 5 213.252.224.238 (213.252.224.238) 43.368 ms 43.399 ms \n" " 5 84.15.6.226 (84.15.6.226) 43.676 ms\n" " \n" " 6 84.15.6.226 (84.15.6.226) 43.460 ms \n" " 6 213.226.129.50 (213.226.129.50) 40.203 ms \n" " 6 84.15.6.226 (84.15.6.226) 43.329 ms\n\n" " 7 s1v3.e-lietuva.lt (141.136.40.34) 40.225 ms \n" " 7 213.226.129.50 (213.226.129.50) 40.106 ms 40.082 ms") 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