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

import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String[] args) { final String regex = "\\b(Sydney|Melbourne|Brisbane|Adelaide)\\b\\s[\\W\\s\\r]{0,10}[0-9]{4}\\,\\s(?<TimeIB>[0-9:APM]{6,7})[\\W\\s\\r]{1,5}[\\W\\s\\rA-Za-z0-9]{5,180}Payment|\\b(Sydney|Melbourne|Brisbane|Adelaide)\\b\\sSUBSIDIARY\\/FRANCHISE[\\W\\s\\r]{0,10}[0-9]{4}\\,\\s(?<TimeIB2>[0-9:APM]{6,7})[\\W\\s\\r]{1,5}[\\W\\s\\rA-Za-z0-9]{5,180}Payment"; final String string = "E-Ticket Itinerary, Receipt and Tax Invoice\n\n" + "Your Booking Reference\n\n" + "OML4CW\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 Matthew Davis\n\n" + "QF1911660387\n" + "Frequent Flyer Silver / oneworld Ruby\n\n" + "081-2498583545\n\n" + "30 Aug 19\n\n" + "Ticket Total*\n" + "423.32\n\n" + "Mr Zachary Kirkham\n\n" + "QF1752890\n" + "Qantas Club Silver / oneworld Ruby\n\n" + "081-2498583544\n\n" + "30 Aug 19\n\n" + "423.32\n\n" + "Ticket Total for all passengers*\n\n" + "846.64\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" + "08 Sep 19\n\n" + "QF435\n\n" + "Sydney\n" + "1300, 1:00PM\n" + "Terminal 3\n\n" + "Melbourne\n" + "1435, 2:35PM\n" + "08 Sep 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" + "08 Sep 19\n\n" + "QF2285 Operated By\n" + "Melbourne\n" + "SUBSIDIARY/FRANCHISE 1520, 3:20PM\n" + "Terminal 1\n\n" + "Launceston\n" + "1635, 4:35PM\n" + "08 Sep 19\n\n" + "Economy\n" + "Confirmed\n\n" + "Est journey Time: 01:15\n" + "Non-Stop\n" + "Aircraft Type: De Havilland Dhc-8 Dash\n" + "8-400 Dash 8q\n\n" + "Payment Details\n" + "Date\n\n" + "Payment Type\n\n" + "Reference\n\n" + "30 Aug 19\n\n" + "American Express\n\n" + "xxxx-xxxx-xxxx-2006\n\n" + "Amount*\n" + "846.64\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" + "761.68\n" + "8.00\n\n" + "GST\n\n" + "Total*\n\n" + "769.68\n\n" + "76.96\n\n" + "846.64\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 30 Aug 19\n\n" + "GST Paid 76.96\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" + "QF435\n\n" + "Sydney to Melbourne - 08 Sep 2019\n" + "PASSENGER\n\n" + "Mr Matthew Davis\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" + "-\n\n" + "Cabin bag combination + 1 personal item\n\n" + "1 x 32kg/70lb\n\n" + "-"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); final Matcher matcher = pattern.matcher(string); while (matcher.find()) { System.out.println("Full match: " + matcher.group(0)); for (int i = 1; i <= matcher.groupCount(); i++) { System.out.println("Group " + i + ": " + matcher.group(i)); } } } }

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 Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html