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
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
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • 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

r"
"
gm

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"^[0-9].+[a-zA-Z]+$" test_str = ("Dear student, \n\n" "Please find your College bill attached. If, after reading the following notes, you have any queries about your bill, then contact studentbilling@christs.cam.ac.uk \n\n" "Explanatory Notes to your College Bill\n\n" "Fees\n\n" "Fees will appear as “University Fees” or “College Fees” as appropriate. If all or part of your fees are funded by a fee loan from the Student Loans Company or by a sponsor this portion of your fees will not appear on your bill. Fees for home and EU undergraduate students are charged 25%, 25%, 50% in Michaelmas, Lent and Easter terms respectively, with 1/3 fees for all other students charged each term. Sponsors will receive one bill in Michaelmas term each year.\n\n" "Catering Charges\n\n" "If you take meals in formal hall or pay for meals or drinks using your university card in Upper Hall or the Buttery or New Court Bars these will appear on your bill in the following term as “Catering charges – “ with the term name appended. This may include charges for guest rooms if you have used them and any charges incurred in the library. The items that make up this charge can be reviewed on the meal booking system on the College intranet by clicking on the ‘EPOS’ tab. If you have a query about your catering charges please contact cateringoffice@christs.cam.ac.uk*\n\n" "Prepayment Kitchen Estimates\n\n" "In your first term at Christs College you will be charged an estimate against the charges you may make during that term, which will be labelled “Catering Account estimate – “, with the term name appended.\n\n" "In the next term you will see an entry that reverses this estimate, labelled Catering Account estimate returned – “, with the term indicating when it was charged and a new estimate charged for the forthcoming term. This new estimate will be based on your actual expenditure in the previous term unless your actual expenditure was under £50, in which case you will not be charged an estimate. NB Graduate students are not charged an estimate on their Long Vacation bills, when their Easter Term estimate is returned.\n\n" "Accommodation charges\n\n" "Accommodation charges will be labelled “Rent”, followed by the period covered. Undergraduates termly rent covers the ‘normal period of residence’. Any accommodation provided outside of this will be charged on a separate line. If you have a query about rent charges on your bill contact accommodation@christs.cam.ac.uk *\n\n" "Graduate students generally pay a holding deposit for accommodation prior to arriving at College. This will be credited to your first bill providing that you take up the accommodation that has been booked for you.\n\n" "College Awards\n\n" "Bursaries and academic awards will appear as a credit on your bill, labelled “Christ’s College Award “ with the name of the award if applicable. Depending on the award it will either be credited termly or in Michaelmas Term. If PhD students’ awards are greater than any fees charged to their account the excess of award over fees will be split in four and credited in Michaelmas, Lent and Easter terms and Long Vacation, with the balance split to match the pattern of fee charges.\n\n" "Scholars whose course continues the following year receive an award of £100 in Lent term.\n\n" "Charity Subscriptions\n\n" "The student body at Christ’s has a proud record of fundraising for those less fortunate than themselves. One way in which this is done is by opting to have ‘charity subs’ added to your college bill. If you opt into this a charge of £5 in Michaelmas, Lent and Easter term will appear on your bill. If you opt in you are free to opt out at any time in the future. A ballot is held in Michaelmas Term to decide which charities will benefit from this.\n\n" "Other Charges\n\n" "There are various other charges that may appear on your bill that should be self-explanatory, such as boat club subs (if you have been a member of the Boat Club), lost university cards, book awards, scholars’ photos and so on.\n\n" "*If your query is with regard to the billing of these charges, for example if you think you have been billed twice for something, then contact studentbilling@christs.cam.ac.uk rather than using the relevant departmental email address.") matches = re.finditer(regex, test_str, re.MULTILINE) for matchNum, match in enumerate(matches, start=1): print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group())) for groupNum in range(0, len(match.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.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