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"(?<=\+)[0-9\-]*" test_str = ("/+1-541-754-3010 156 Alphand_St. <J Steeve>\n" " 133, Green, Rd. <E Kustur> NY-56423 ;+1-541-914-3010\n" "+1-541-984-3012 <P Reed> /PO Box 530; Pollocksville, NC-28573\n" " :+1-321-512-2222 <Paul Dive> Sequoia Alley PQ-67209\n" "+1-741-984-3090 <Peter Reedgrave> _Chicago\n" " :+1-921-333-2222 <Anna Stevens> Haramburu_Street AA-67209\n" "+1-111-544-8973 <Peter Pan> LA\n" " +1-921-512-2222 <Wilfrid Stevens> Wild Street AA-67209\n" "<Peter Gone> LA ?+1-121-544-8974 \n" " <R Steell> Quora Street AB-47209 +1-481-512-2222\n" "<Arthur Clarke> San Antonio $+1-121-504-8974 TT-45120\n" " <Ray Chandler> Teliman Pk. !+1-681-512-2222! AB-47209,\n" "<Sophia Loren> +1-421-674-8974 Bern TP-46017\n" " <Peter O'Brien> High Street +1-908-512-2222; CC-47209\n" "<Anastasia> +48-421-674-8974 Via Quirinal Roma\n" " <P Salinger> Main Street, +1-098-512-2222, Denver\n" "<C Powel> *+19-421-674-8974 Chateau des Fosses Strasbourg F-68000\n" " <Bernard Deltheil> +1-498-512-2222; Mount Av. Eldorado\n" "+1-099-500-8000 <Peter Crush> Labrador Bd.\n" " +1-931-512-4855 <William Saurin> Bison Street CQ-23071\n" "<P Salinge> Main Street, +1-098-512-2222, Denve\n" "<P Salinge> Main Street, +1-098-512-2222, Denve\n" "/+5-541-754-3010 156 Alphandria_Street. <Jr Part>\n" " 1333, Green, Road <F Fulgur> NW-46423 ;+6-541-914-3010!\n" "+5-541-984-3012 <Peter Reeves> /PO Box 5300; Albertville, SC-28573\n" " :+5-321-512-2222 <Paulo Divino> Boulder Alley ZQ-87209\n" "+3-741-984-3090 <F Flanaghan> _Chicago Av.\n" " :+3-921-333-2222 <Roland Scorsini> Bellevue_Street DA-67209\n" "+8-111-544-8973 <Laurence Pantow> SA\n" " +8-921-512-2222 <Raymond Stevenson> Joly Street EE-67209\n" "<John Freeland> Mantow ?+2-121-544-8974 \n" " <Robert Mitch> Eleonore Street QB-87209 +2-481-512-2222?\n" "<Arthur Paternos> San Antonio $+7-121-504-8974 TT-45121\n" " <Ray Charles> Stevenson Pk. !+7-681-512-2222! CB-47209,\n" "<JP Gorce> +9-421-674-8974 New-Bern TP-16017\n" " <P McDon> Revolution Street +2-908-512-2222; PP-47209\n" "<Elizabeth Corber> +8-421-674-8974 Via Papa Roma\n" " <C Saborn> Main Street, +15-098-512-2222, Boulder\n" "<Colin Marshall> *+9-421-674-8974 Edinburgh UK\n" " <Bernard Povit> +3-498-512-2222; Hill Av. Cameron\n" "+12-099-500-8000 <Pete Highman> Ontario Bd.\n" " +8-931-512-4855 <W Mount> Oxford Street CQ-23071\n" "<Donald Drinkaw> Moon Street, +3-098-512-2222, Peterville") 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