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

$re = '/<slb:ReturnCode>(?<ReturnCode>.*?)<\/slb:ReturnCode>.*?(?:<slb:Message>(?<FailureMessage>.*?)<\/slb:Message>|<slb:SearchResultsCount>(?<Count>\d)<\/slb:SearchResultsCount>).*?<slb:LocationId>(?<LocationId>.*?)<\/slb:LocationId>/m'; $str = '<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:slb="http://www.csgsystems.com/slbos"><soapenv:Header /><soapenv:Body><slb:RetrieveAccountResponse><slb:head><slb:Client>ElnkFive9IOT</slb:Client><slb:RoutingArea>8024</slb:RoutingArea><slb:Region>QAHA</slb:Region><slb:Source>IVR</slb:Source><slb:Destination>CCS</slb:Destination><slb:Version>17.4</slb:Version><slb:TransactionId>16b2120c-032d-11eb-810f-005056b21df3</slb:TransactionId><slb:ServerId>OTPRESLBWCF04</slb:ServerId></slb:head><slb:Response><slb:ReturnCode>0000</slb:ReturnCode></slb:Response><slb:SearchResultsCount>1</slb:SearchResultsCount><slb:SearchResults><slb:Region>QAHA</slb:Region><slb:Business><slb:BusinessUnit>802440000360</slb:BusinessUnit></slb:Business><slb:Customer><slb:CustomerId>1101520678264</slb:CustomerId></slb:Customer><slb:Account><slb:AccountId>8024400360069535</slb:AccountId><slb:Classification> </slb:Classification><slb:Type>RES</slb:Type><slb:ConnectStatus> </slb:ConnectStatus><slb:Name><slb:Salutation>U</slb:Salutation><slb:First>CONTRACTOR</slb:First><slb:Last>PAY ESS+</slb:Last></slb:Name><slb:Phone><slb:Home>9023427424</slb:Home></slb:Phone><slb:LineOfBusinessCount>5</slb:LineOfBusinessCount><slb:LineOfBusiness><slb:Type>C</slb:Type><slb:TypeDescription>VIDEO </slb:TypeDescription><slb:Status>A</slb:Status></slb:LineOfBusiness><slb:LineOfBusiness><slb:Type>I</slb:Type><slb:TypeDescription>HSD </slb:TypeDescription><slb:Status>A</slb:Status></slb:LineOfBusiness><slb:LineOfBusiness><slb:Type>T</slb:Type><slb:TypeDescription>TELEPHONY</slb:TypeDescription><slb:Status>N</slb:Status></slb:LineOfBusiness><slb:LineOfBusiness><slb:Type>Y</slb:Type><slb:TypeDescription>WIRELESS </slb:TypeDescription><slb:Status>N</slb:Status></slb:LineOfBusiness><slb:LineOfBusiness><slb:Type>Z</slb:Type><slb:TypeDescription>SECURITY </slb:TypeDescription><slb:Status>N</slb:Status></slb:LineOfBusiness><slb:Balance><slb:Current>304.34</slb:Current><slb:Projected>304.34</slb:Projected><slb:Statement>304.34</slb:Statement><slb:Highest>298.00</slb:Highest></slb:Balance><slb:PPVCreditLimit>250.00</slb:PPVCreditLimit><slb:Payment><slb:SubType> </slb:SubType><slb:SubTypeStatus> </slb:SubTypeStatus><slb:History>10Z </slb:History><slb:LastDate>0001-01-01</slb:LastDate><slb:LastAmount>0.00</slb:LastAmount><slb:FutureDated>N</slb:FutureDated></slb:Payment><slb:BillTo><slb:Name><slb:Salutation>U</slb:Salutation><slb:First>CONTRACTOR</slb:First><slb:Last>PAY ESS+</slb:Last></slb:Name><slb:StatementNameFormat>2</slb:StatementNameFormat><slb:Address><slb:Line1>7 REGRESSION TESTING ST</slb:Line1><slb:City>HALIFAX</slb:City><slb:State>NS</slb:State><slb:PostalCode>B2M0A2</slb:PostalCode><slb:ForceIndicator>1</slb:ForceIndicator><slb:Country>CAN</slb:Country></slb:Address><slb:AddressInheritanceIndicator>Y</slb:AddressInheritanceIndicator></slb:BillTo><slb:Billing><slb:CycleDay>14</slb:CycleDay><slb:FromDate>2020-09-14</slb:FromDate><slb:ThruDate>2020-10-13</slb:ThruDate><slb:Frequency>01</slb:Frequency><slb:TaxGroup>99</slb:TaxGroup><slb:DemandStatement>N</slb:DemandStatement><slb:StatementHold>N</slb:StatementHold><slb:EBPP>N</slb:EBPP><slb:Electronic>N</slb:Electronic><slb:ElectronicOutsourced>N</slb:ElectronicOutsourced><slb:Hardcopy>Y</slb:Hardcopy></slb:Billing><slb:Identification><slb:TaxIdentifier>XXXXXXXXX</slb:TaxIdentifier><slb:TaxType>X</slb:TaxType><slb:PIN>01234</slb:PIN></slb:Identification><slb:Delinquency><slb:Days>045</slb:Days><slb:Status> </slb:Status><slb:Amount>183.89</slb:Amount><slb:ChargeOff><slb:Reason slb:Index="1"><slb:Code>00</slb:Code><slb:Description>Misappld Pymnt</slb:Description></slb:Reason><slb:Amount>0.00</slb:Amount></slb:ChargeOff><slb:DunningGroup>030</slb:DunningGroup><slb:Cycle1><slb:Occurences>001</slb:Occurences><slb:Amount>183.89</slb:Amount></slb:Cycle1></slb:Delinquency><slb:MonetaryStatus>D</slb:MonetaryStatus><slb:SalesRepresentative>0</slb:SalesRepresentative><slb:ConnectDate>2020-08-14</slb:ConnectDate><slb:DisconnectDate>2020-08-04</slb:DisconnectDate><slb:DisconnectReason>NT</slb:DisconnectReason><slb:DisconnectReasonDescription>No Truck Roll</slb:DisconnectReasonDescription><slb:LastMonetaryType>S</slb:LastMonetaryType><slb:LastMonetaryDate>2020-09-15</slb:LastMonetaryDate><slb:LastNonMonetaryDate>2020-09-24</slb:LastNonMonetaryDate><slb:AutoPackageIndicator>P</slb:AutoPackageIndicator><slb:RestrictTroubleCall>N</slb:RestrictTroubleCall><slb:RestrictPPV>N</slb:RestrictPPV><slb:CustomField slb:Index="2"><slb:Name>RETIREE ID </slb:Name><slb:Value> </slb:Value></slb:CustomField><slb:CustomField slb:Index="3"><slb:Name>COM TEST1 </slb:Name><slb:Value> </slb:Value></slb:CustomField><slb:CustomField slb:Index="4"><slb:Name>COMM CLASS </slb:Name><slb:Value> </slb:Value></slb:CustomField><slb:CustomField slb:Index="5"><slb:Name>ARMSTRONG ACCT</slb:Name><slb:Value> </slb:Value></slb:CustomField><slb:CustomField slb:Index="6"><slb:Name>NO COLLECTIONS</slb:Name><slb:Value> </slb:Value></slb:CustomField><slb:CustomField slb:Index="7"><slb:Name>COLLECT LETTER</slb:Name><slb:Value> </slb:Value></slb:CustomField><slb:CustomField slb:Index="8"><slb:Name>CONTRACT (ER) </slb:Name><slb:Value> </slb:Value></slb:CustomField><slb:CustomField slb:Index="9"><slb:Name>BWOS # </slb:Name><slb:Value> </slb:Value></slb:CustomField><slb:CustomField slb:Index="10"><slb:Name>BANKRUPTCY </slb:Name><slb:Value> </slb:Value></slb:CustomField><slb:CustomField slb:Index="11"><slb:Name>COLLECTIONS </slb:Name><slb:Value> </slb:Value></slb:CustomField><slb:CustomField slb:Index="12"><slb:Name>PAY ARRANGEMEN</slb:Name><slb:Value> </slb:Value></slb:CustomField><slb:CustomField slb:Index="13"><slb:Name>MIND ACCT # </slb:Name><slb:Value> </slb:Value></slb:CustomField><slb:CustomField slb:Index="14"><slb:Name>AM COLLECTOR #</slb:Name><slb:Value> </slb:Value></slb:CustomField><slb:CustomField slb:Index="15"><slb:Name>LOW USAGE </slb:Name><slb:Value> </slb:Value></slb:CustomField><slb:CustomField slb:Index="16"><slb:Name>LEGACY ACCT# </slb:Name><slb:Value> </slb:Value></slb:CustomField><slb:Audit><slb:Creation><slb:Date>2020-08-04</slb:Date></slb:Creation></slb:Audit></slb:Account><slb:Location><slb:LocationId>11222010200191</slb:LocationId><slb:Type>AA</slb:Type><slb:TypeDescription>Residence</slb:TypeDescription><slb:Address><slb:Line1>7 REGRESSION TESTING ST</slb:Line1><slb:City>HALIFAX</slb:City><slb:State>NS</slb:State><slb:PostalCode>112220102</slb:PostalCode></slb:Address><slb:LineOfBusinessCount>4</slb:LineOfBusinessCount><slb:LineOfBusiness><slb:Type>C</slb:Type><slb:TypeDescription>VIDEO </slb:TypeDescription><slb:Status>A</slb:Status></slb:LineOfBusiness><slb:LineOfBusiness><slb:Type>I</slb:Type><slb:TypeDescription>HSD </slb:TypeDescription><slb:Status>A</slb:Status></slb:LineOfBusiness><slb:LineOfBusiness><slb:Type>T</slb:Type><slb:TypeDescription>VOICE </slb:TypeDescription><slb:Status>A</slb:Status></slb:LineOfBusiness><slb:LineOfBusiness><slb:Type>Z</slb:Type><slb:TypeDescription>SECURITY </slb:TypeDescription><slb:Status>N</slb:Status></slb:LineOfBusiness><slb:Drop><slb:Tag slb:Index="1"><slb:Identifier>MONIQUE</slb:Identifier></slb:Tag></slb:Drop></slb:Location></slb:SearchResults></slb:RetrieveAccountResponse></soapenv:Body></soapenv:Envelope>'; preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0); // Print the entire match result var_dump($matches);

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 PHP, please visit: http://php.net/manual/en/ref.pcre.php