Regular Expressions 101

Save & Share

  • Regex Version: ver. 1
  • Update Regex
    ctrl+⇧+s
  • Save new Regex
    ctrl+s
  • Add to Community Library

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"^[A-Z\d]+\s+\d{4}-\d{2}-\d{2}\s+\w+(?:[ -]\w+)*\s+\S+\s+\S+\s+(\S+)" test_str = ("2020-08-26 PILE OF LIFE HEALTH PRODUCTS LP Page 1 A/P Remittance Advice Direct Deposit 2020-08-26\n\n" "Cheque # 11361 Vendor # 0828 HAIN CELESTIAL CANADA, ULC\n\n" "Invoice # Date Description Gross Disc Net\n" "===============================================================\n\n" "225299 2020-07-24 P2156678 7,610.52 .00 7,610.52 \n" "225839 2020-07-22 P2157105 7,826.28 .00 7,826.28 \n" "225969 2020-07-22 P2157106 8,760.59 .00 8,760.59 \n" "226384 2020-07-22 P2157104 42,274.76 .00 42,274.76\n" "CR01BEPJ 2020-08-17 MULTI MCBS 4,470.06- .00 4,470.06-\n" "CR01BEXS 2020-08-24 MULTI MCBS 5,212.81- .00 5,212.81-\n\n" "DM20082311 2020-08-14 LIFESTYLE MARKETS NA 201.25- .00 201.25-\n" "DM20083583 2020-08-17 KARDISH FOOD FRANCHI 281.37- .00 281.37-\n" "DM20085965 2020-08-12 AVRILSUPERMARCHE-WAR 871.50- .00 871.50-\n" "DM20086678 2020-08-12 AVRILSUPERMARCHE-WAR 871.50- .00 871.50-\n" "DM20089459 2020-07-30 LOBLAWS 8.90- .00 8.90- \n" "DM20089500 2020-08-14 COUNTRY GROCER-CHASE 105.00- .00 105.00-\n\n" "========================================== 54,449.76 .00 54,449.76\n\n" "Printed on 2020-08-26 at 6:26\n\n" "2020-04-23 PILE OF LIFE HEALTH PRODUCTS LP Page 1 A/P Remittance Advice Direct Deposit 2020-04-23\n\n" "Cheque # 9699 Vendor # 0828 HAIN CELESTIAL CANADA, ULC\n\n" "Invoice # Date Description Gross Disc Net ===================================================================================\n\n" "218124 2020-02-27 P2151168 2,253.44 .00 2,253.44 \n" "219021 2020-03-18 P2152030 35,242.65 .00 35,242.65\n" "219216 2020-03-18 P2152031 8,306.81 .00 8,306.81 \n" "CR01BASW 2020-04-20 MULTI MCBS 5,278.05- .00 5,278.05-\n\n" "DM2004W450 2020-04-17 RETURNS WFM-GR-20589 124.63- .00 124.63-\n" "DM2004W828 2020-04-17 RETURNS WFM-GR-20589 266.09- .00 266.09-\n" "DM20042157 2020-04-07 AVRIL 871.50- .00 871.50-\n" "DM20043798 2020-04-07 COUNTRY GROCER 105.00- .00 105.00-\n" "DM20043892 2020-04-07 COUNTRY GROCER 105.00- .00 105.00-\n" "DM20048663 2020-04-07 AVRIL 871.50- .00 871.50-\n" "DM20049986 2020-04-02 LA MOISSON 258.69- .00 258.69-\n\n" "========================================== 37,922.44 .00 37,922.44\n\n" "Printed on 2020-04-23 at 13:13") 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