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

/
/
gi

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"(on|date)*\s*\,*(mon|tue|wed|thu|fri|sat)\,*\s*([0-9]{2,4}|([0-9]{2}:*){1,3})\s+(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s+([0-9]{2,4}|([0-9]{2}:*){1,3})\s+(([0-9]{2}:*){1,3}|[0-9]{2,4})\s*((\+|\-)+(([0-9]{2}:*){1,3}|[0-9]{2,4}))*\n") test_str = ("From ilug-admin@linux.ie Wed Aug 21 13:33:23 2002\n" "Return-Path: <ilug-admin@linux.ie>\n" "Delivered-To: yyyy@localhost.netnoteinc.com\n" "Received: from localhost (localhost [127.0.0.1])\n" " by phobos.labs.netnoteinc.com (Postfix) with ESMTP id E0EBC43C34\n" " for <jm@localhost>; Wed, 21 Aug 2002 08:33:21 -0400 (EDT)\n" "Received: from phobos [127.0.0.1]\n" " by localhost with IMAP (fetchmail-5.9.0)\n" " for jm@localhost (single-drop); Wed, 21 Aug 2002 13:33:21 +0100 (IST)\n" "Received: from lugh.tuatha.org (root@lugh.tuatha.org [194.125.145.45]) by\n" " dogma.slashnull.org (8.11.6/8.11.6) with ESMTP id g7LCYUZ24675 for\n" " <jm-ilug@jmason.org>; Wed, 21 Aug 2002 13:34:30 +0100\n" "Received: from lugh (root@localhost [127.0.0.1]) by lugh.tuatha.org\n" " (8.9.3/8.9.3) with ESMTP id NAA29496; Wed, 21 Aug 2002 13:33:16 +0100\n" "X-Authentication-Warning: lugh.tuatha.org: Host root@localhost [127.0.0.1]\n" " claimed to be lugh\n" "Received: from salmon.maths.tcd.ie (mmdf@salmon.maths.tcd.ie\n" " [134.226.81.11]) by lugh.tuatha.org (8.9.3/8.9.3) with SMTP id NAA29463\n" " for <ilug@linux.ie>; Wed, 21 Aug 2002 13:33:07 +0100\n" "Received: from walton.maths.tcd.ie by salmon.maths.tcd.ie with SMTP id\n" " <aa96976@salmon>; 21 Aug 2002 13:33:06 +0100 (BST)\n" "To: ilug@linux.ie\n" "Subject: Re: [ILUG] URGENT: Cant get a skrew out... PLEASE HELP!\n" "X-It'S: all good\n" "X-Wigglefluff: fuddtastic\n" "X-Zippy: When this load is DONE I think I'll wash it AGAIN..\n" "In-Reply-To: Your message of\n" " \"Wed, 21 Aug 2002 12:42:17 BST.\"\n" " <BCEFLMCEIJHPCPLGADJICEDPCAAA.kialllists@redpie.com>\n" "Date: Wed, 21 Aug 2002 13:33:06 +0100\n" "From: Niall Brady <bradyn@maths.tcd.ie>\n" "Message-Id: <200208211333.aa96976@salmon.maths.tcd.ie>\n" "Sender: ilug-admin@linux.ie\n" "Errors-To: ilug-admin@linux.ie\n" "X-Mailman-Version: 1.1\n" "Precedence: bulk\n" "List-Id: Irish Linux Users' Group <ilug.linux.ie>\n" "X-Beenthere: ilug@linux.ie\n\n" "On Wed, 21 Aug 2002 12:42:17 BST, Kiall Mac Innes said:\n" ">Hi i have a phillips head skrew thats holding a circut board together i need\n" ">to take it out ASAP and nothing will work, the threads on the skrew are\n" ">almost completly gone, its is a very small skrew that i have to use a\n" ">percision skrewdriver set to remove the skrews any help would be\n" ">appreaciated...\n\n" "Get a very, *very* small set of drill bits. Start drilling right\n" "through the center of the head, at a slow speed so you don't pop\n" "off and through the board! Once you have a bit of an indent in,\n" "increase the speed a little bit.\n\n" "If you've made a deepish indent, and the head doesn't pop off, use\n" "the next largest drill bit you have. Repeat until happy.\n\n" "Eventually the head should just pop off, allowing you to lift the\n" "board off over the shaft of the screw... then ye can take out the\n" "rest fairly easily with a pliers or whatnot.\n\n" "-- \n" " Niall\n\n" "-- \n" "Irish Linux Users' Group: ilug@linux.ie\n" "http://www.linux.ie/mailman/listinfo/ilug for (un)subscription information.\n" "List maintainer: listmaster@linux.ie\n\n") matches = re.finditer(regex, test_str, re.IGNORECASE) 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