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

/
/
g

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"(?<level>\d+)\s*(?<wood>\d+)\s*(?<clay>\d+)\s*(?<iron>\d+)\s*(?<crop>\d+)\s*(?<cropusage>\d+)\s*(?<time>\d+:\d+:\d+)\s*(?<pop>\d+)\s*(?<production>\d+)" test_str = ("1 70 90 70 20 0 00:02:30 1 7\n" "2 115 150 115 35 0 00:07:20 1 13\n" "3 195 250 195 55 0 00:15:00 2 21\n" "4 325 420 325 95 0 00:27:30 2 31\n" "5 545 700 545 155 0 00:47:10 2 46\n" "6 910 1170 910 260 1 01:18:50 3 70\n" "7 1520 1950 1520 435 1 02:03:40 4 98\n" "8 2535 3260 2535 725 1 03:30:40 4 140\n" "9 4235 5445 4235 1210 1 05:40:30 5 203\n" "10 7070 9095 7070 2020 1 09:08:00 6 280\n" "11 11810 15185 11810 3375 1 14:40:10 7 392\n" "12 19725 25360 19725 5635 1 23:31:40 9 525\n" "13 32940 42350 32940 9410 1 37:41:50 11 693\n" "14 55005 70720 55005 15715 1 60:22:20 13 889\n" "15 91860 118105 91860 26245 1 96:39:10 15 1120\n" "16 153405 197240 153405 43830 2 154:41:50 18 1400\n" "17 256190 329385 256190 73195 2 247:34:20 22 1820\n" "18 427835 550075 427835 122240 2 396:10:10 27 2240\n" "19 714485 918625 714485 204140 2 633:550: 32 2800\n" "20\n" " 1193195 1534105 1193195 340915 2 1014:20:30 38 3430") subst = "<Level val=\"${level}\"><Resources><Wood>${wood}</Wood><Clay>${clay}</Clay><Iron>${iron}</Iron><Crop>${crop}</Crop><CropUsage>${cropusage}</CropUsage></Resources><UpgradeTime>${time}</UpgradeTime><Population>${pop}</Population><Production>${production}</Production></Level>" # You can manually specify the number of replacements by changing the 4th argument result = re.sub(regex, subst, test_str, 0) 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