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

/
/
m

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"passengers\*\s+?(?<TotalPrice>[0-9\,\.]{5,8})|Total\*\s+?([0-9\,\.]{5,8})" test_str = ("E-Ticket Itinerary, Receipt and Tax Invoice\n\n" "Your Booking Reference\n\n" "KTHQEJ\n" "Important Information\n" "• This is your E-Ticket Itinerary & Receipt/Tax Invoice. We recommend that you print this out or save this on your phone, and it\n" "is recommended you retain a copy for your records.\n" "• Each passenger travelling needs a printed copy of this document for airport security checks.\n" "• Please familiarise yourself with the key Conditions of Carriage, Dangerous Goods guide and other information attached.\n" "Passenger Ticket Information\n" "Passenger Name\n\n" "Frequent Flyer No.\n\n" "Ticket No.\n\n" "Issued\n\n" "Mr Zachary Kirkham\n\n" "QF1752890\n" "Qantas Club Silver / oneworld Ruby\n\n" "081-2301896427\n\n" "04 Dec 19\n\n" "Ticket Total*\n" "814.07\n\n" "Mr Matthew Davis\n\n" "QF1911660387\n" "Frequent Flyer Silver / oneworld Ruby\n\n" "081-2301896426\n\n" "04 Dec 19\n\n" "814.07\n\n" "Ticket Total for all passengers*\n\n" "1,628.14\n\n" "*Amounts are displayed in AUD (AUD)\n\n" "Your Itinerary\n" "Date\n\n" "Flight Number\n\n" "Departing\n\n" "Arriving\n\n" "Status\n\n" "Flight Information\n\n" "09 Dec 19\n\n" "QF471\n\n" "Sydney\n" "0615, 6:15AM\n" "Terminal 3\n\n" "Melbourne\n" "0750, 7:50AM\n" "09 Dec 19\n" "Terminal 1\n\n" "Economy\n" "Confirmed\n\n" "Est journey Time: 01:35\n" "Non-Stop\n" "Aircraft Type: Boeing 737\n\n" "11 Dec 19\n\n" "QF434\n\n" "Melbourne\n" "1300, 1:00PM\n" "Terminal 1\n\n" "Sydney\n" "1425, 2:25PM\n" "11 Dec 19\n" "Terminal 3\n\n" "Economy\n" "Confirmed\n\n" "Est journey Time: 01:25\n" "Non-Stop\n" "Aircraft Type: Boeing 737\n\n" "Payment Details\n" "Date\n\n" "Payment Type\n\n" "Reference\n\n" "04 Dec 19\n\n" "American Express\n\n" "xxxx-xxxx-xxxx-2006\n\n" "Amount*\n" "1,628.14\n\n" "This may appear as multiple transactions on your credit card statement\n\n" "Your Receipt Details\n" "Ticket Charges\n" "Total*\n" "Card Payment Fee\n\n" "Total Amount Payable*\n\n" "Charges\n" "1,465.04\n" "15.10\n\n" "GST\n\n" "Total*\n\n" "1,480.14\n\n" "148.00\n\n" "1,628.14\n\n" "*Includes Taxes/Fees/carrier Charges\n\n" "Tax Information\n" "GST is included for taxable sales\n" "ABN:58084409316\n\n" "Issued by\n" "Qantas Airways on 04 Dec 19\n\n" "GST Paid 148.00\n\n" "Flying With Us\n" "Before Check-in\n" "• Ensure that each passenger\n" "carries a printed copy of this\n" "document when travelling.\n" "• Label your bags inside and out\n" "with your name, address and\n" "contact number at your\n" "destination.\n" "• Check Carry-on baggage and\n" "checked baggage allowances as\n" "restrictions apply.\n\n" "Getting Away On Time\n" "• Ensure you are at the airport with\n" "enough time to complete necessary\n" "check-in, security screening and,\n" "for international flights, customs\n" "and immigration.\n" "• Familiarise yourself with the checkin and boarding times. Information\n" "for Qantas and QantasLink flights is\n" "in the Travel Information section\n" "attached.\n\n" "Check-in\n" "• Visit qantas.com/checkin to\n" "choose the check-in option best\n" "for you.\n\n" "Fare Restrictions - Refer to the applicable fare rules as special conditions may apply\n" "Additional Fees: Fees may apply to some booking changes, ticket reissues and consultant-assisted services. Details are available at qantas.com.\n\n" "Qantas Airways Limited ABN 16 009 661 901, 10 Bourke Road, Mascot NSW 2020, Australia\n" "qantas.com\n\n" " Travel Information\n" "Qantas Group check-in times\n\n" "International Terminal\n\n" "Domestic Terminal\n\n" "Australian and New Zealand Terminals**\n\n" "QF 001 - 399\n\n" "QF400 and above\n\n" "International First / Business\n" "International Economy / Premium Economy\n\n" "Closes 60 minutes before departure\n" "Closes 90 minutes before departure\n\n" "Domestic Business / Economy\n\n" "Closes 60 minutes before departure\n\n" "Qantas codeshare services (QF Flight\n" "numbers operated by Jetstar)^\n" "Jetstar Services^\n\n" "Check in opens 3 hours before departure\n" "Closes 60 minutes before departure\n" "JQ 001 - 399 and 3K 500 - 699^\n\n" "N/A\n" "N/A\n" "45 minutes from departure* Closes 30\n" "minutes before departure.\n" "Check in opens 2 hours before departure\n" "Closes 30 minutes before departure\n" "JQ 400 and above\n\n" "Jetstar Business / Economy (JQ)^\n\n" "Check in opens 3 hours before departure\n" "Closes 60 minutes before departure\n" "Check in opens 2 hours before departure\n\n" "Check in opens 2 hours before departure\n" "Closes 30 minutes before departure\n" "N/A\n\n" "Jetstar flights operated by Jetstar Asia (3K)~\n\n" "* Check-in closes 30 minutes before departure. Boarding will commence 20 minutes prior to departure. ** Please check with your local Qantas office for check-in\n" "time at all non-Australian / New Zealand ports. Ensure you adhere to check-in times or your fare may be forfeited.\n" "^ Jetstar flights JQ1-JQ399 depart from international terminals, except JQ61, JQ73 and JQ81 which depart from domestic terminals.\n" "~ Jetstar flights that operate under a 3K flight number between Perth and Singapore and Singapore and Auckland check-in opens 3 hours prior to departure and\n" "closes 1 hour prior to departure.\n\n" "Included and Carry On Baggage\n\n" "QF471\n\n" "Sydney to Melbourne - 09 Dec 2019\n" "PASSENGER\n\n" "Mr Zachary\n" "Kirkham\n\n" "INCLUDED CHECKED BAGGAGE\n" "(no single piece may exceed 32kg / 67lb)\n\n" "PURCHASED BAGGAGE\n" "(no single piece may exceed 32kg / 67lb)\n\n" "CARRY ON BAGGAGE\n" "(Up to 10kg(22lb) for one piece, max 14kg(30lb) total)\n\n" "1 x 32kg/70lb\n\n" "1 x 32kg/70lb\n\n" "Cabin bag combination + 1 personal item\n\n" "1 x 32kg/70lb\n\n" "-\n\n" "Cabin bag combination + 1 personal item\n\n" "Silver QC - 1752890 Adult\n\n" "Mr Matthew Davis\n" "Silver - 1911660387 Adult\n\n" "Domestic Economy Checked + Carry On baggage information\n" "CHECKED BAGGAGE\n\n" "CARRY ON\n\n" "+\n\n" "L + H + D = 140cm/54in\n" "No single item of checked\n" "baggage may exceed 32kg\n" "(70lb). Dimensions for\n" "checked baggage are\n" "calculated by adding\n" "together the width, height\n" "and depth of the piece of\n" "baggage. For tickets\n" "containing a flight to or from\n" "the Americas, the amount is\n" "the total of all the baggage\n" "added together.\n\n" "Carry-on baggage varies depending on destination, class of travel or aircraft. One small personal item could be a\n" "handbag, laptop computer or overcoat. Restrictions on height, depth, length and weight apply to carry-on\n" "baggage. More information is on the Carry-on Baggage page.\n\n" "Qantas Airways Limited ABN 16 009 661 901, 10 Bourke Road, Mascot NSW 2020, Australia\n" "qantas.com\n\n" " QF434\n\n" "Melbourne to Sydney - 11 Dec 2019\n" "PASSENGER\n\n" "Mr Zachary\n" "Kirkham\n\n" "INCLUDED CHECKED BAGGAGE\n" "(no single piece may exceed 32kg / 67lb)\n\n" "PURCHASED BAGGAGE\n" "(no single piece may exceed 32kg / 67lb)\n\n" "CARRY ON BAGGAGE\n" "(Up to 10kg(22lb) for one piece, max 14kg(30lb) total)\n\n" "1 x 32kg/70lb\n\n" "1 x 32kg/70lb\n\n" "Cabin bag combination + 1 personal item\n\n" "1 x 32kg/70lb\n\n" "-\n\n" "Cabin bag combination + 1 personal item\n\n" "Silver QC - 1752890 Adult\n\n" "Mr Matthew Davis\n" "Silver - 1911660387 Adult\n\n" "Domestic Economy Checked + Carry On baggage information\n" "CHECKED BAGGAGE\n\n" "CARRY ON\n\n" "+\n\n" "L + H + D = 140cm/54in\n" "No single item of checked\n" "baggage may exceed 32kg\n" "(70lb). Dimensions for\n" "checked baggage are\n" "calculated by adding\n" "together the width, height\n" "and depth of the piece of\n" "baggage. For tickets\n" "containing a flight to or from\n" "the Americas, the amount is\n" "the total of all the baggage\n" "added together.\n\n" "Carry-on baggage varies depending on destination, class of travel or aircraft. One small personal item could be a\n" "handbag, laptop computer or overcoat. Restrictions on height, depth, length and weight apply to carry-on\n" "baggage. More information is on the Carry-on Baggage page.\n\n" "Qantas Airways Limited ABN 16 009 661 901, 10 Bourke Road, Mascot NSW 2020, Australia\n" "qantas.com\n\n" " Travel Information\n" "Important Information\n" "Dimensions:\n" "International Flights (excluding North and South America):\n" "Total dimensions for each piece must not exceed 158cm (62in). Dimensions for checked baggage are calculated by adding\n" "together the width, height and depth of the piece of baggage.\n" "International Flights to and from North & South America:\n" "Total dimensions of the 3 pieces must not exceed 405cm (159in) and total dimensions of the 2 pieces must not exceed\n" "270cm (106in) with no single piece exceeding 158cm (62in). Dimensions for checked baggage are calculated by adding together\n" "the width, height and depth of the piece of baggage.\n" "Domestic Flights:\n" "Total dimensions of each piece must not exceed 140cm (55in). Dimensions for checked baggage are calculated by adding\n" "together the width, height and depth of the piece of baggage.\n" "+Adults travelling with an infant (under 2 years) on Qantas operated services may check-in up to three infant items\n" "free of charge. See here for more details.\n" "Interline travel: Qantas baggage allowance may not apply. If your travel on this ticket involves more than one\n" "airline, the baggage allowance of the most significant carrier in your itinerary will apply to each journey, except in\n" "relation to travel to, from or within the USA. Under US DOT regulations, if your travel to, from or within the USA\n" "involves more than one airline, the baggage rules of one airline will apply to all of the flights in your itinerary. This is\n" "determined by the first carrier in your itinerary. For travel between Australia and the USA, usually (both not always)\n" "Qantas' baggage allowance will apply to the whole itinerary.\n" "Member Allowances: Qantas Frequent Flyer and Qantas Club member baggage allowances apply to travel on\n" "Qantas and QantasLink operated flights with a QF flight number on your ticket, excluding flights to or from Lord Howe\n" "Island and Mount Hotham. These allowances also apply to Emirates operated flights with an EK flight number\n" "between and within Europe, the Middle East, North Africa, Asia and Australia. These benefits are not cumulative.\n" "Definitions: Domestic travel is travel that is not combined with any international flights on the same ticket.\n" "International travel is travel between two or more countries including any flight within those countries if it is combined\n" "with the international travel.\n" "General: No single item of checked baggage may exceed 32kg (70lb). Carriage of baggage is subject to space availability and\n" "any applicable aircraft weight restrictions, which vary. Baggage exceeding the specified allowance may need to be\n" "accommodated on a later flight.\n" "Visit qantas.com/baggage or the \"Planning & Booking - Baggage\" page of jetstar.com for important information\n" "regarding baggage restrictions. For additional and excess baggage rates visit qantas.com/additionalbaggage or\n" "jetstar.com.\n\n" "Qantas Airways Limited ABN 16 009 661 901, 10 Bourke Road, Mascot NSW 2020, Australia\n" "qantas.com\n\n" " Travel Information\n" "Enhanced Security Screening - Powders, Liquids, aerosols and gels\n" "Enhanced security measures apply to and from Australia and on domestic\n" "sectors of international flights within Australia. Similar or more restrictive\n" "measures may apply for other countries. If you want to take powders, liquids,\n" "aerosols or gels through the screening point, make sure:\n" "• each item is 100ml or less;\n" "• all items fit comfortably in a transparent resealable 1 litre plastic\n" "bag (only 1 bag per person is allowed)\n\n" "Travelling with Money\n" "Are you planning to carry funds in or out of Australia? By law, you must now:\n" "• If asked by a Customs or police officer, report travellers\n" "cheques, cheques, money orders or any other bearer\n" "negotiable instruments of any amount.\n" "• Always report A$10,000 cash or more (or foreign currency\n" "equivalent) using a form available from Customs.\n" "Visit www.austrac.gov.au for more information.\n\n" "Exceptions:Prescription and non-prescription medicines and baby products\n" "that you need for the flight, but please note:\n" "• proof of need may be required, and\n" "• additional security checks may be performed\n" "• All powders must be screened separately with restrictions on\n" "the carriage of inorganic powders over 350 millilitres (350\n" "grams)\n\n" "Qantas Privacy Collection Notice\n" "Qantas collects information about you (including health information where\n" "necessary) to provide products and services to you, facilitate your\n" "participation in our and other organisations' loyalty programs, ensure the\n" "safety and security of all passengers when travelling with us, conduct\n" "marketing activities for our and third parties' products and services and\n" "conduct market research.\n\n" "Note - Duty Free items: Duty free liquids, aerosols and gels not exceeding\n" "100ml per item may be taken onboard in your transparent resealable 1 litre\n" "plastic bag. Government screening requirements vary between countries\n" "around the world, and duty free liquids, aerosols and gels which exceed\n" "100ml may not always be permitted through a screening point at all airports on\n" "your journey. If your journey includes a flight with a transit stop or a transfer\n" "to an onward connecting flight, you may be required to take these items\n" "through the transit or transfer screening points where you may be required to\n" "surrender the item, and it will not be returned to you.\n\n" "We may collect your personal information from people who make or update\n" "your travel booking or otherwise interact with us on your behalf, from our\n" "related bodies corporate and Jetstar branded entities, from our service\n" "providers and from immigration, customs, border security and other regulatory\n" "authorities. Some of the information we collect is required under the Customs\n" "Act 1901 (Cth). If the information is not provided, we may not be able to\n" "provide the service requested.\n\n" "Qantas recommends that you visit ") matches = re.search(regex, test_str, re.MULTILINE) 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