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 = '/^.*<SRVCNAME>(?<SRVCNAME>[\/\w\?=]+)(<\/SRVCNAME>)?((<MESSAGETYPE>(?<MESSAGETYPE>.+?)<\/MESSAGETYPE>)?)?(<PROCESSORNAME>(?<PROCESSORNAME>.+)<\/PROCESSORNAME>)?(<CLNTIP>(?<CLNTIP>.+)<\/CLNTIP>)?(<TRANID>(?<TRANID>.+)<\/TRANID>)?(<TIMESTAMP>(?<TIMESTAMP>.+)<\/TIMESTAMP>)?(<TIMEELAPSED>(?<TIMEELAPSED>.+)<\/TIMEELAPSED>)?(<DOMAINNAME>(?<DOMAINNAME>.+)<\/DOMAINNAME>)?(<DEVICESERIALNUM>(?<DEVICESERIALNUM>.+)<\/DEVICESERIALNUM>)?((<INPUTMSG>)?(?<INPUTMSG>.*)(:?<\/INPUTMSG>.*))?(<\/CommonLoggingService>)?(?<line_breaker>(\<\?xml version="1\.0" encoding="UTF-8"\?>)?)?/m'; $str = '<CommonLoggingService><SRVCNAME>/apij/mme/telematics/GetAlertHistory</SRVCNAME><MESSAGETYPE>response</MESSAGETYPE><PROCESSORNAME>EAI-ROUTER-MPG</PROCESSORNAME><CLNTIP>53.30.178.253:36682</CLNTIP><TRANID>477312335</TRANID><TIMESTAMP>2017-10-05T11:04:50-04:00</TIMESTAMP><TIMEELAPSED>360</TIMEELAPSED><DOMAINNAME>EAIProd</DOMAINNAME><DEVICESERIALNUM>7803112</DEVICESERIALNUM><INPUTMSG>{"Data":{"Alerts":[],"SpeedAlerts":[]},"Response":{"ResponseCode":"1003","ResponseDescription":"No Records Found"}}</INPUTMSG></CommonLoggingService> <CommonLoggingService><SRVCNAME>/apij/mme/aftersales/service/services/vmilite/4JGDA0EB4FA472807</SRVCNAME><MESSAGETYPE>response</MESSAGETYPE><PROCESSORNAME>EAI-ROUTER-MPG</PROCESSORNAME><CLNTIP>53.30.178.250:36721</CLNTIP><TRANID>533680495</TRANID><TIMESTAMP>2017-10-05T11:05:12-04:00</TIMESTAMP><TIMEELAPSED>505</TIMEELAPSED><DOMAINNAME>EAIProd</DOMAINNAME><DEVICESERIALNUM>7803070</DEVICESERIALNUM><INPUTMSG>{"vin":"4JGDA0EB4FA472807","model":"ML250BTC","year":"2015","engineNo":"65196032390143","transmission":"72290810204344","prodDate":"08/29/2014","sellingDealer":"14146","primaryStatus":"NORMAL","warSale":{"wsStrDate":"10/30/2014"}}</INPUTMSG></CommonLoggingService>e><SRVCNAME>/apij/mme/telematics/QueryAccountDetails</SRVCNAME><MESSAGETYPE>response</MESSAGETYPE><PROCESSORNAME>EAI-ROUTER-MPG</PROCESSORNAME><CLNTIP>53.30.178.252:36695</CLNTIP><TRANID>222825457</TRANID><TIMESTAMP>2017-10-05T11:05:12-04:00</TIMESTAMP><TIMEELAPSED>804</TIMEELAPSED><DOMAINNAME>EAIProd</DOMAINNAME><DEVICESERIALNUM>7803112</DEVICESERIALNUM><INPUTMSG>{"context":{"SourceName":"WEB","TransactionId":"6K5U4YTE3023E1VDMW5Q8MU6ZT5S3R","Timestamp":"2017-10-05T15:05:10.583Z","Organization":"Mercedes-Benz","Region":"US","ApplicationName":"Android"},"Data":{"CustomerTier":"Customer","ESAFlag":"M","AccountName":"MBRACE FLEET","Address":{"AddressDetails":[{"Address1":"2002 SUMMIT BLVD","Address2":"","City":"BROOKHAVEN","StateProvince":"GA","Country":"USA","County":"GA","ZipCode":"30319-1560","AddressType":"Mailing Address","PrimaryAddressFlag":"Y"}]},"Phone":{"PrimaryPhone":{"PhoneNumber":"4047800377","CountryCode":""},"CellPhone":{"PhoneNumber":"","CountryCode":""},"HomePhone":{"PhoneNumber":"","CountryCode":""},"WorkPhone":{"PhoneNumber":"","CountryCode":""},"Fax":{"PhoneNumber":"","CountryCode":""},"PrimaryPhoneType":""},"EMail":{},"Contact":{"Name":{"FirstName":"MBRACE","MiddleName":"","LastName":"FLEET","Prefix":"","Suffix":"","PersonUId":"1-5346007"},"Address":{"AddressDetails":[{"Address1":"2002 SUMMIT BLVD","Address2":"","City":"BROOKHAVEN","StateProvince":"GA","Country":"USA","County":"","ZipCode":"30319-1560","AddressType":"Mailing Address","PrimaryAddressFlag":"Y","Longitude":"","Latitude":""}]},"Phone":{"PrimaryPhone":{"PhoneNumber":"4047800377","CountryCode":""},"CellPhone":{"PhoneNumber":"4047800377","CountryCode":""},"HomePhone":{"PhoneNumber":"","CountryCode":""},"WorkPhone":{"PhoneNumber":"4047800377","CountryCode":""},"Fax":{"PhoneNumber":"","CountryCode":""},"PrimaryPhoneType":"Mobile"},"Email":{"Email":[{"EmailAddress":"dele.nonyejohn@verizon.com"}]},"ContactType":"Primary","PreferredContactMethod":"Email Address","AlternateContactMethod":"Home Phone #","ServiceMSGContactMethod":"Email","ServiceMSGPhoneNumber":"Mobile Phone #","ServiceMSGWirelessCarrier":"","TimeZone":"","OptionalFlag":"N","PIN":"8744","Comments":""},"AccountNumber":"38184765","AccountType":"Subscriber","AccountStatus":"Active","PreferredDealer":"","AANFlag":"Y","SalesDealer":"","EmergencyFirstName":"","EmergencyLastName":"","EmergencyPhone":"","Relationship":""},"Response":{"ResponseCode":"00","ResponseStatus":"SUCCESS","ResponseDescription":"ACCOUNT INFORMATION FOUND."}}</INPUTMSG></CommonLoggingService><?xml version="1.0" encoding="UTF-8"?> <CommonLoggingService><SRVCNAME>/apij/mme/customer/services/finAccountInfo/?vin=WDC0J4KB1HF232726</SRVCNAME><MESSAGETYPE>response</MESSAGETYPE><PROCESSORNAME>EAI-ROUTER-MPG</PROCESSORNAME><CLNTIP>53.30.178.248:36776</CLNTIP><TRANID>533647919</TRANID><TIMESTAMP>2017-10-05T11:04:50-04:00</TIMESTAMP><TIMEELAPSED>220</TIMEELAPSED><DOMAINNAME>EAIProd</DOMAINNAME><DEVICESERIALNUM>7803070</DEVICESERIALNUM><INPUTMSG> <CommonLoggingService><SRVCNAME>/apij/mme/aftersales/service/services/vmilite/WDC0G4JB0HV006098</SRVCNAME><MESSAGETYPE>request</MESSAGETYPE><PROCESSORNAME>EAI-ROUTER-MPG</PROCESSORNAME><CLNTIP>53.30.178.251:33618</CLNTIP><TRANID>477313711</TRANID><TIMESTAMP>2017-10-05T11:04:50-04:00</TIMESTAMP><TIMEELAPSED>1</TIMEELAPSED><DOMAINNAME>EAIProd</DOMAINNAME><DEVICESERIALNUM>7803112</DEVICESERIALNUM><INPUTMSG/></CommonLoggingService>e><?xml version="1.0" encoding="UTF-8"?> <CommonLoggingService><SRVCNAME>/apij/mme/telematics/VehicleTra<?xml version="1.0" encoding="UTF-8"?> <CommonLoggingService><SRVCNAME>/apij/mme/telematics/VehicleTrackingStatus</SRVCNAME><MESSAGETYPE>response</MESSAGETYPE><PROCESSORNAME>EAI-ROUTER-MPG</PROCESSORNAME><CLNTIP>53.30.178.248:33990</CLNTIP><TRANID>221204193</TRANID><TIMESTAMP>2017-10-05T11:22:26-04:00</TIMESTAMP><TIMEELAPSED>377</TIMEELAPSED><DOMAINNAME>EAIProd</DOMAINNAME><DEVICESERIALNUM>7803070</DEVICESERIALNUM><INPUTMSG>{"context":{"SourceName":"WEB","TransactionId":"CGYWNXEGF9RDY7VDEJUYN8S2HESDAB","Timestamp":"2017-10-05T15:22:24.772Z","Organization":"Mercedes-Benz","Region":"US","ApplicationName":"Android"},"Data":{"trackingMode":"STOP","trackingInterval":0},"Response":{"ResponseCode":"2000","ResponseStatus":"Success","ResponseDescription":"Successful Execution"}}</INPUTMSG></CommonLoggingService>-ROUTER-MPG</PROCESSORNAME><CLNTIP>53.30.178.250:36775</CLNTIP><TRANID>223870097</TRANID><TIMESTAMP>2017-10-05T11:22:26-04:00</TIMESTAMP><TIMEELAPSED>995</TIMEELAPSED><DOMAINNAME>EAIProd</DOMAINNAME><DEVICESERIALNUM>7803112</DEVICESERIALNUM><INPUTMSG>{"Response":{"Tracing":{"TraceID":"JA6RH30553DV1O4WL2U638O4C2MVF3","TraceTimestamp":"2017-10-05T11:22:25.656Z"},"ResponseCode":"2000","ResponseStatus":"Success","ResponseDescription":"Successful Execution"}}</INPUTMSG></CommonLoggingService><?xml version="1.0" encoding="UTF-8"?>'; 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