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 (5)

Tools

Sponsors
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
No Match

r"
"
i

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"(52.\d{3}(-\d*)?)(\s?)((([\w\W]*?\.)(\s*?)(\w[\s\S]*?)(\s*?)(\(End of (clause|provision)\)))|(\[Reserved\]))" test_str = ("52.209-6Protecting theGovernment’s Interest WhenSubcontracting withContractorsDebarred, Suspended, or Proposedfor Debarment.\n" "As prescribed in 9.409, insert the following clause:\n" "PROTECTING THE GOVERNMENT’S INTEREST WHEN SUBCONTRACTING WITH CONTRACTORS DEBARRED, SUSPENDED, OR PROPOSED FOR DEBARMENT (OCT 2015)\n" "(a) Definition.“Commercially available off-the-shelf (COTS)” item,as used in this clause(\n" "1) Means any item of supply (including construction material) that is(\n" "i)Acommercial item (as defined in paragraph(1)of the definition in FAR 2.101);\n" "(ii) Sold in substantial quantities in the commercial marketplace; and\n" "(iii) Offered tothe Government, under a contract or subcontract at any tier, without modification, in the same form in which it is sold in the commercial marketplace; and\n" "(2) Does not include bulk cargo, as defined in 46 U.S.C. 40102(4), such as agricultural products and petroleum products.\n" "52.2-49\n" "52.209-7 FEDERAL ACQUISITION REGULATION\n" "(b)\n" "The Government suspends or debarsContractors to protect the Government’s interests. Other than a subcontract for a commercially available off-the-shelf item, the Contractorshallnot enter into any subcontract, in excessof $35,000 with a Contractor that is debarred, suspended, or proposed for debarment by any executive agency unless there is a compelling reason to do so.\n" "(c)\n" "The Contractor shall require each proposed subcontractor whose subcontract will exceed $35,000, other than a subcontractor providing acommercially available off-the-shelfitem, todiscloseto the Contractor, inwriting, whether as of thetime ofaward of thesubcontract, the subcontractor, or itsprincipals,is or is not debarred,suspended, orproposed for debarment by the Federal Government.\n" "(d)\n" "A corporate officer or a designeeof the Contractor shall notify the Contracting Officer, in writing, beforeentering into a subcontract witha party (other than a subcontractor providinga commercially available off-the-shelf item) that is debarred, suspended, or proposed for debarment (see FAR 9.404 for information on the System for Award Management (SAM) Exclusions). The notice must include the following:\n" "(1)\n" "The name of the subcontractor.\n" "(2)\n" "The Contractor’s knowledge of the reasons for the subcontractor being listed with an exclusion inSAM.\n" "(3)\n" "The compelling reason(s) for doing business with the subcontractor notwithstanding its being listed with an exclusion in SAM.\n" "(4)\n" "The systems and procedures the Contractor has established to ensure that it is fully protecting the Government's interests whendealing with such subcontractor in view of the specific basisforthe party’sdebarment,suspension, or proposed debarment.\n" "(e)\n" "Subcontracts. Unless this is a contract for the acquisition of commercial items, the Contractor shall include the requirements of this clause, including this paragraph (e) (appropriately modified for the identification of the parties), in each subcontract that(\n" "1)\n" "Exceeds $35,000 in value; and\n" "(2)\n" "Is not a subcontract for commercially available off-the-shelfitems.\n" "(End of clause)") matches = re.search(regex, test_str, re.IGNORECASE) if matches: print ("Match was found at {start}-{end}: {match}".format(start = matches.start(), end = matches.end(), match = matches.group())) for groupNum in range(0, len(matches.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = matches.start(groupNum), end = matches.end(groupNum), group = matches.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