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

/
/
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 = "(?<Month>\\d{1,2})\\/(?<Day>\\d{1,2})\\/(?<Year>(?:\\d{4}|\\d{2}))"; final String string = "--- Getting started with Expresso ---\n\n" + "Try clicking the \"Run Match\" button (or F5) to see what\n" + "happens. After a successful match click in the results box\n" + "to highlight the matched text. Click the \"Replace\" button\n" + "using the \"Dates\" example. Notice in the results box that the\n" + "format of all the dates has been altered. Using the\n" + "\"Expression Library\" tab double-click another regular\n" + "expression and \"Run Match\".\n\n" + "Click \"Show the Builder\" (or Ctrl+D) to start designing your\n" + "own regular expressions; or enter them directly in the\n" + "regular expression box. Enter the text you want to search in\n" + "the sample text box.\n\n" + "Expand nodes in the \"Regex Analyzer\" to see a description\n" + "of the current regular expression. Right-click a node to edit\n" + "the expression.\n\n" + "--- Sample text ---\n" + "Comma Separated Values\n" + "a,b,c,d,e\n" + "Dog,Cat,Puppy\n\n" + "US phone numbers, (800) 325-3535, (650) 555 1212\n" + "US zip codes, 95076-1234, 90210-6473\n" + "Dates:12/25/02 9/11/2001 11/22/63 12/7/41\n\n" + "URLs:\n" + "http://www.usgs.gov\n" + "http://www.acl.lanl.gov/URI/archive/uri-archive.index.html\n" + "ftp://@host.com/\n" + "ftp://host.com/\n" + "ftp://foo:@host.com/\n" + "ftp://myname@host.dom/%2Fetc/motd\n" + "file://vms.host.edu/disk$user/my/notes/note12345.txt\n" + "prospero://host.dom//pros/name\n\n" + "IP addresses:\n" + "123.54.63.0\n" + "255.257.0.1 -> This one has an illegal number\n" + "0.255.255.12\n\n" + "UK Postal Codes:\n" + "M1 1AA \n" + "M60 1NW \n" + "ABD3 7BB -> This is an illegal format\n" + "M1 1CA -> This has illegal letters (CIKMOV) in the second half\n" + "W1A 1HQ \n\n" + "Canadian Postal Codes:\n" + "A1A 1A1\n" + "B6C 8X7\n" + "5 Shoreham Drive, Downsview, Ontario, M3N 1S4\n\n" + "Netherlands Postal Codes:\n" + "1234 ZA\n" + "5678 KL\n" + "0123 AZ -> Illegal number, must be 1000-9999\n" + "5432 ABQ -> Too many characters\n\n" + " \n" + "Here is some text as bait for the multiple captures example:\n" + "XYZAbcAbcAbcXYZAbcAb\n" + "acacacac\n\n" + " Paris\n" + " in the\n" + "the spring\n\n" + "{a[b(c)d]e}\n\n" + "Email:\n" + "esupport@ultrapico.com\n" + "maynard@krebs.com\n\n" + "Hyperlinks\n" + "<a href=\"http://www.codeproject.com\">The Code Project</a>\n" + "Href=\"http://ultrapico.com/Expresso.htm\"\n\n" + "Key-Value Pairs\n" + "FurryAnimal=Dog\n" + "NonFurryAnimal=Turtle\n\n" + "HTML Tags\n" + "<p>Hello World</p>\n" + "<H1>Regular Expressions</H1>\n\n" + "Social Security Numbers\n" + "123-45-6789\n" + "987-65-4321\n\n" + "Mastercard\n" + "5134567890123456\n" + "Visa\n" + "4234567890123\n" + "Amex\n" + "343456789012345\n" + "Diners Club\n" + "30045678901234\n" + "Discover\n" + "6011567890123456\n\n" + "Numbers\n" + "123.56\n" + "-234.5\n" + "12\n" + "+13\n" + "0.56\n" + "-.2\n" + ".3\n" + "25.\n" + "1.38E-23\n" + "0.2E45\n" + "6E+19\n" + "0AFD\n" + "1CA0234F"; 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