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

/
/

Test String

Code Generator

Generated Code

$re = '/<(?:Attendance)[^>]+\sBookingReference\=\"0317000067\b[^>]*>/'; $str = '<?xml version="1.0" encoding="utf-8"?> <!--MSW.MAPS.Extractor, Version=10.6.301.17, Culture=neutral, PublicKeyToken=null--> <!--Started at 2017-03-29 10:35:03Z--> <HealthRosterExtract> <HDR FileName="TA__MSW_MN1802_000009.DAT" CreationDate="20170329103503" SystemSourceCode="MSW" TrustIdentifier="" PeriodType="M" ProcessType="N" ExtractType="AttendanceAndAbsence" HistoricPeriods="0" CostCentreSource="Duty" InhibitSubjectiveCodes="False" ExcludeVPD="False" AggregrateTimeAndAttendanceData="False" SequenceNumber="9" PayChannel="Generic Bank" ExtractRunID="762" OrgUnit="DL Ward" ExtractBatch="" IsTestExtract="False" TaxPeriod="2" PeriodValidFrom="20170201000000" PeriodValidTo="20170228000000" /><ATT><Attendance EffectiveDate="" OperationType="Add" StaffNumber="87654321" AssignmentNumber="87654321" Surname="Bank" Forenames="Generic" EarnedDate="20170227" ElementName="Band 5 Mon - Fri H" ElementValueName1="Units to Pay" ElementValueOption1="FixedValue" ElementValueText1="Pay Hours" ElementValueName2="Rate of Pay" ElementValueOption2="FixedValue" ElementValueText2="18.32" ElementValueName3="" ElementValueOption3="NotInUse" ElementValueText3="" ElementValueName4="" ElementValueOption4="NotInUse" ElementValueText4="" ElementValueName5="" ElementValueOption5="NotInUse" ElementValueText5="" ElementValueName6="" ElementValueOption6="NotInUse" ElementValueText6="" ElementValueName7="" ElementValueOption7="NotInUse" ElementValueText7="" ElementValueName8="" ElementValueOption8="NotInUse" ElementValueText8="" CostCentreCode="12345" DutyCostCentreCode="12345" DutyCostCentreName="Dom Ward" DutyCostCentreExternalRef="" PostingCostCentreCode="" PostingCostCentreName="" PostingCostCentreExternalRef="" PayHours="7.50" WorkHours="7.50" Occurances="1.00" NegotiatedRate="0.00" SubjectiveCode="" DateWorked="20170227" GradeExternalReference="Band5RN" UnitExternalReference="" AllowanceUnits="0" ExternalAbsenceType="" ExternalAbsenceCode="" ExternalAbsenceReference="" DutyAssignmentStartDateTime="20170227070000" DutyAssignmentEndDateTime="20170227150000" BreakdownStartDateTime="20170227070000" BreakdownEndDateTime="20170227150000" UnavailabilityStartDateTime="" UnavailabilityEndDateTime="" UnavailabilityHasOverlappingCancelledDuties="False" DutyCancelReason="" BookingReference="0317000043" BookingMethod="ManualManagerAssignment" /><Attendance EffectiveDate="" OperationType="Add" StaffNumber="76543211" AssignmentNumber="76543211" Surname="Bank" Forenames="Daily" EarnedDate="20170228" ElementName="Band 5 Mon - Fri D" ElementValueName1="Units to Pay" ElementValueOption1="FixedValue" ElementValueText1="Occurrence" ElementValueName2="Rate of Pay" ElementValueOption2="FixedValue" ElementValueText2="200" ElementValueName3="" ElementValueOption3="NotInUse" ElementValueText3="" ElementValueName4="" ElementValueOption4="NotInUse" ElementValueText4="" ElementValueName5="" ElementValueOption5="NotInUse" ElementValueText5="" ElementValueName6="" ElementValueOption6="NotInUse" ElementValueText6="" ElementValueName7="" ElementValueOption7="NotInUse" ElementValueText7="" ElementValueName8="" ElementValueOption8="NotInUse" ElementValueText8="" CostCentreCode="12345" DutyCostCentreCode="12345" DutyCostCentreName="Dom Ward" DutyCostCentreExternalRef="" PostingCostCentreCode="" PostingCostCentreName="" PostingCostCentreExternalRef="" PayHours="7.50" WorkHours="7.50" Occurances="1.00" NegotiatedRate="0.00" SubjectiveCode="" DateWorked="20170228" GradeExternalReference="Band5RN" UnitExternalReference="" AllowanceUnits="0" ExternalAbsenceType="" ExternalAbsenceCode="" ExternalAbsenceReference="" DutyAssignmentStartDateTime="20170228070000" DutyAssignmentEndDateTime="20170228150000" BreakdownStartDateTime="20170228070000" BreakdownEndDateTime="20170228150000" UnavailabilityStartDateTime="" UnavailabilityEndDateTime="" UnavailabilityHasOverlappingCancelledDuties="False" DutyCancelReason="" BookingReference="0317000045" BookingMethod="ManualManagerAssignment" /> <Attendance EffectiveDate="" OperationType="Add" StaffNumber="76543211" AssignmentNumber="76543211" Surname="Bank" Forenames="Daily" EarnedDate="20170228" ElementName="Band 5 Mon - Fri D" ElementValueName1="Units to Pay" ElementValueOption1="FixedValue" ElementValueText1="Occurrence" ElementValueName2="Rate of Pay" ElementValueOption2="FixedValue" ElementValueText2="200" ElementValueName3="" ElementValueOption3="NotInUse" ElementValueText3="" ElementValueName4="" ElementValueOption4="NotInUse" ElementValueText4="" ElementValueName5="" ElementValueOption5="NotInUse" ElementValueText5="" ElementValueName6="" ElementValueOption6="NotInUse" ElementValueText6="" ElementValueName7="" ElementValueOption7="NotInUse" ElementValueText7="" ElementValueName8="" ElementValueOption8="NotInUse" ElementValueText8="" CostCentreCode="12345" DutyCostCentreCode="12345" DutyCostCentreName="Dom Ward" DutyCostCentreExternalRef="" PostingCostCentreCode="" PostingCostCentreName="" PostingCostCentreExternalRef="" PayHours="7.50" WorkHours="7.50" Occurances="1.00" NegotiatedRate="0.00" SubjectiveCode="" DateWorked="20170228" GradeExternalReference="Band5RN" UnitExternalReference="" AllowanceUnits="0" ExternalAbsenceType="" ExternalAbsenceCode="" ExternalAbsenceReference="" DutyAssignmentStartDateTime="20170228070000" DutyAssignmentEndDateTime="20170228150000" BreakdownStartDateTime="20170228070000" BreakdownEndDateTime="20170228150000" UnavailabilityStartDateTime="" UnavailabilityEndDateTime="" UnavailabilityHasOverlappingCancelledDuties="False" DutyCancelReason="" BookingReference="0317000067" BookingMethod="ManualManagerAssignment" /></ATT> <FTR RecordCount="2" PayHoursTotal="15.00" StaffNumbersCount="2" AssignmentNumbersCount="2" BookingReferencesCount="2" HashTotal="0" /></HealthRosterExtract><!--Finished at 2017-03-29 10:35:04Z-->'; preg_match($re, $str, $matches, PREG_OFFSET_CAPTURE, 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