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

/
/
gs

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"\s*<(attribute|measure)(?=.+?id=\"(\w+)\").+?>(?=.+?columnName=\"(?:4Z\w+_)?(\w+)\").*?<\/\1>" test_str = (" <attribute id=\"VENDOR_NUMBER\" order=\"1\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Vendor\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0VENDOR\"/>\n" " </attribute>\n" " <attribute id=\"VENDOR_NAME\" order=\"2\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Vendor Name (Text)\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0VENDOR___T\"/>\n" " </attribute>\n" " <attribute id=\"COMPANY_CODE\" order=\"3\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Company Code\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0COMP_CODE\"/>\n" " </attribute>\n" " <attribute id=\"COMPANY_CODE_TXT\" order=\"4\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Company Code (Text)\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0COMP_CODE___T\"/>\n" " </attribute>\n" " <attribute id=\"CREATED_BY\" order=\"5\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Created By\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0CREATEDBY\"/>\n" " </attribute>\n" " <attribute id=\"DATE_CREATED_TXT\" order=\"6\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Date Created (Text)\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0CREATEDON\"/>\n" " </attribute>\n" " <attribute id=\"DOCUMENT_TYPE\" order=\"8\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Document Type\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0DOCTYPE\"/>\n" " </attribute>\n" " <attribute id=\"DOCUMENT_CATEGORY\" order=\"9\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Document Category\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0DOC_CAT\"/>\n" " </attribute>\n" " <attribute id=\"DOCUMENT_CURRENCY\" order=\"10\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Document Currency\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0DOC_CURRCY\"/>\n" " </attribute>\n" " <attribute id=\"DOCUMENT_CUR_TXT\" order=\"11\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Document Currency (Text)\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0DOC_CURRCY___T\"/>\n" " </attribute>\n" " <attribute id=\"DOCUMENT_DATE_TXT\" order=\"12\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Document Date (Text)\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0DOC_DATE\"/>\n" " </attribute>\n" " <attribute id=\"PO_DOCUMENT_NUMBER\" order=\"14\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Document Number\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0DOC_NUM\"/>\n" " </attribute>\n" " <attribute id=\"INCOTERMS_1\" order=\"15\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Incoterms Part 1\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0INCOTERMS\"/>\n" " </attribute>\n" " <attribute id=\"INCOTERMS_1_TXT\" order=\"16\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Incoterms Part 1 (Text)\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0INCOTERMS___T\"/>\n" " </attribute>\n" " <attribute id=\"INCOTERMS_2\" order=\"17\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Incoterms Part 2\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0INCOTERMS2\"/>\n" " </attribute>\n" " <attribute id=\"INVOICE_PARTY\" order=\"18\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Invoicing Party\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0INV_PARTY\"/>\n" " </attribute>\n" " <attribute id=\"INVOICE_PARTY_TXT\" order=\"19\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Invoicing Party (Text)\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0INV_PARTY___T\"/>\n" " </attribute>\n" " <attribute id=\"MODE_OF_TRANSPORTATION\" order=\"20\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Mode of Transport\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0OI_MOT\"/>\n" " </attribute>\n" " <attribute id=\"ORIGIN_COUNTRY\" order=\"21\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Country of Origin\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0OI_OLAND1\"/>\n" " </attribute>\n" " <attribute id=\"ORGIN_REGION_OR_STATE\" order=\"22\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Region or State of Origin\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0OI_OREGIO\"/>\n" " </attribute>\n" " <attribute id=\"PAYMENT_TERMS\" order=\"23\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Terms of Payment\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0PMNTTRMS\"/>\n" " </attribute>\n" " <attribute id=\"PAYMENT_TERMS_TXT\" order=\"24\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Terms of Payment (Text)\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0PMNTTRMS___T\"/>\n" " </attribute>\n" " <attribute id=\"PURCHASING_ORG\" order=\"25\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Purchasing Organization\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0PURCH_ORG\"/>\n" " </attribute>\n" " <attribute id=\"PURCHASING_ORG_TXT\" order=\"26\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Purchasing Organization (Text)\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0PURCH_ORG___T\"/>\n" " </attribute>\n" " <attribute id=\"PURCHASING_GROUP\" order=\"27\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Purchasing Group\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0PUR_GROUP\"/>\n" " </attribute>\n" " <attribute id=\"PURCHASING_GROUP_TXT\" order=\"28\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Purchasing Group (Text)\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0PUR_GROUP___T\"/>\n" " </attribute>\n" " <attribute id=\"DESTINATION_COUNTRY\" order=\"29\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Destination Country\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0RECIPCNTRY\"/>\n" " </attribute>\n" " <attribute id=\"DESTINATION_COUNTRY_TXT\" order=\"30\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Destination Country (Text)\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0RECIPCNTRY___T\"/>\n" " </attribute>\n" " <attribute id=\"SUPPLYING_PLANT\" order=\"31\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Supplying Plant\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0SUPP_PLANT\"/>\n" " </attribute>\n" " <attribute id=\"SUPPLYING_PLANT_TXT\" order=\"32\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Supplying Plant (Text)\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0SUPP_PLANT___T\"/>\n" " </attribute>\n" " <attribute id=\"RELEASE_IND\" order=\"33\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Release Indicator: Purchasing Document\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"4ZMMCP012_FRGKE\"/>\n" " </attribute>\n" " <attribute id=\"REL_NOT_COMPLETLY_EFFECTED\" order=\"34\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Release Not Yet Completely Effected\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"4ZMMCP012_FRGRL\"/>\n" " </attribute>\n" " <attribute id=\"YOUR_REFERENCE\" order=\"35\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Your Reference\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"4ZMMCP012_IHREZ\"/>\n" " </attribute>\n" " <attribute id=\"OUR_REFERENCE\" order=\"36\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Our Reference\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"ZP_UNSEZ\"/>\n" " </attribute>\n" " <attribute id=\"EXCH_RATE_FIXED_INDICATOR\" order=\"37\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Indicator: Fixing of Exchange Rate\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"4ZMMCP012_KUFIX\"/>\n" " </attribute>\n" " <attribute id=\"PO_PROCESSING_STATUS\" order=\"38\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Purchasing Document Processing State\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"4ZMMCP012_PROCSTAT\"/>\n" " </attribute>\n" " <attribute id=\"TOT_VALUE_AT_FULL_PO_APPROVAL\" order=\"39\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Total Value at Time of Release\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"4ZMMCP012_RLWRT\"/>\n" " </attribute>\n" " <attribute id=\"VAT_REGISTRATION_NUMBER\" order=\"40\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"VAT Registration Number\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"4ZMMCP012_STCEG\"/>\n" " </attribute>\n" " <attribute id=\"COLLECTIVE_NUMBER\" order=\"41\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Collective Number\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"4ZMMCP012_SUBMI\"/>\n" " </attribute>\n" " <attribute id=\"RELEASE_STATUS\" order=\"42\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Release Status\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"ZP_FRGZU\"/>\n" " </attribute>\n" " <attribute id=\"RELEASE_GROUP\" order=\"43\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Release Group\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"ZRELGRP\"/>\n" " </attribute>\n" " <attribute id=\"RELEASE_STRATEGY\" order=\"44\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Release Strategy\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"ZRELSTR\"/>\n" " </attribute>\n" " <attribute id=\"EXCHANGE_RATE\" order=\"45\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"Exchange Rate for Pricing &amp; Statistics\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"0EXCHG_RATE\"/>\n" " </attribute>\n" " <attribute id=\"__AP_CHECK_INT\" order=\"47\" attributeHierarchyActive=\"false\" displayAttribute=\"false\" transparentFilter=\"false\">\n" " <descriptions defaultDescription=\"__AP_CHECK_INT\"/>\n" " <keyMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"__AP_CHECK_INT\"/>\n" " </attribute>\n" " <measure id=\"ROWCOUNT\" order=\"46\" aggregationType=\"sum\" measureType=\"simple\">\n" " <descriptions defaultDescription=\"Number of Records\"/>\n" " <measureMapping columnObjectName=\"PO_HEADER_UNION\" columnName=\"1ROWCOUNT\"/>\n" " </measure>") subst = "\\3,\\2\\n" # You can manually specify the number of replacements by changing the 4th argument result = re.sub(regex, subst, test_str, 0, re.DOTALL) 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