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

r"
"

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 = "<th(?:| | .*?)>\\s*<a(?:| | .*?)>\\s*([\\d.]+)\\s*</a>\\s*</th>\\s*<td(?:| | .*?)>\\s*stable\\s*</td>"; final String string = " <table cellpadding=\"2\" cellspacing=\"1\" style=\"width: 100%; border: 0px;\">\n" + " <tr style=\"background-color: #CCCCCC;\">\n" + " <th colspan=\"5\">Available Releases</th>\n" + " </tr>\n" + " <tr>\n" + " <th valign=\"top\" bgcolor=\"#ffffff\">Version</th>\n" + " <th valign=\"top\" bgcolor=\"#ffffff\">State</th>\n" + " <th valign=\"top\" bgcolor=\"#ffffff\">Release Date</th>\n" + " <th valign=\"top\" bgcolor=\"#ffffff\">Downloads</th>\n" + " <th valign=\"top\" bgcolor=\"#ffffff\">&nbsp;</th>\n" + " </tr>\n" + " <tr>\n" + " <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/5.1.2\">5.1.2</a></th>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">stable</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">2015-12-07</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-5.1.2.tgz\">apcu-5.1.2.tgz</a> (105.7kB)</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=5.1.2\">Changelog</a>]</small></td>\n" + " </tr>\n" + " <tr>\n" + " <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/4.0.10\">4.0.10</a></th>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">stable</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">2015-12-07</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-4.0.10.tgz\">apcu-4.0.10.tgz</a> (116.5kB)&nbsp;&nbsp;<a href=\"/package/APCu/4.0.10/windows\"><img src=\"/gifs/windows-icon.png\" />DLL</a></td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=4.0.10\">Changelog</a>]</small></td>\n" + " </tr>\n" + " <tr>\n" + " <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/5.1.0\">5.1.0</a></th>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">beta</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">2015-11-21</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-5.1.0.tgz\">apcu-5.1.0.tgz</a> (106.8kB)</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=5.1.0\">Changelog</a>]</small></td>\n" + " </tr>\n" + " <tr>\n" + " <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/4.0.8\">4.0.8</a></th>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">stable</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">2015-11-20</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-4.0.8.tgz\">apcu-4.0.8.tgz</a> (116.3kB)&nbsp;&nbsp;<a href=\"/package/APCu/4.0.8/windows\"><img src=\"/gifs/windows-icon.png\" />DLL</a></td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=4.0.8\">Changelog</a>]</small></td>\n" + " </tr>\n" + " <tr>\n" + " <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/4.0.7\">4.0.7</a></th>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">beta</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">2014-10-11</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-4.0.7.tgz\">apcu-4.0.7.tgz</a> (115.9kB)&nbsp;&nbsp;<a href=\"/package/APCu/4.0.7/windows\"><img src=\"/gifs/windows-icon.png\" />DLL</a></td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=4.0.7\">Changelog</a>]</small></td>\n" + " </tr>\n" + " <tr>\n" + " <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/4.0.6\">4.0.6</a></th>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">beta</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">2014-06-12</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-4.0.6.tgz\">apcu-4.0.6.tgz</a> (116.9kB)&nbsp;&nbsp;<a href=\"/package/APCu/4.0.6/windows\"><img src=\"/gifs/windows-icon.png\" />DLL</a></td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=4.0.6\">Changelog</a>]</small></td>\n" + " </tr>\n" + " <tr>\n" + " <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/4.0.5\">4.0.5</a></th>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">beta</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">2014-06-11</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-4.0.5.tgz\">apcu-4.0.5.tgz</a> (116.9kB)&nbsp;&nbsp;<a href=\"/package/APCu/4.0.5/windows\"><img src=\"/gifs/windows-icon.png\" />DLL</a></td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=4.0.5\">Changelog</a>]</small></td>\n" + " </tr>\n" + " <tr>\n" + " <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/4.0.4\">4.0.4</a></th>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">beta</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">2014-03-01</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-4.0.4.tgz\">apcu-4.0.4.tgz</a> (116.9kB)&nbsp;&nbsp;<a href=\"/package/APCu/4.0.4/windows\"><img src=\"/gifs/windows-icon.png\" />DLL</a></td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=4.0.4\">Changelog</a>]</small></td>\n" + " </tr>\n" + " <tr>\n" + " <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/4.0.3\">4.0.3</a></th>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">beta</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">2014-01-27</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-4.0.3.tgz\">apcu-4.0.3.tgz</a> (116.9kB)&nbsp;&nbsp;<a href=\"/package/APCu/4.0.3/windows\"><img src=\"/gifs/windows-icon.png\" />DLL</a></td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=4.0.3\">Changelog</a>]</small></td>\n" + " </tr>\n" + " <tr>\n" + " <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/4.0.2\">4.0.2</a></th>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">beta</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">2013-09-14</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-4.0.2.tgz\">apcu-4.0.2.tgz</a> (117.5kB)&nbsp;&nbsp;<a href=\"/package/APCu/4.0.2/windows\"><img src=\"/gifs/windows-icon.png\" />DLL</a></td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=4.0.2\">Changelog</a>]</small></td>\n" + " </tr>\n" + " <tr>\n" + " <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/4.0.1\">4.0.1</a></th>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">beta</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">2013-04-30</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-4.0.1.tgz\">apcu-4.0.1.tgz</a> (103.4kB)&nbsp;&nbsp;<a href=\"/package/APCu/4.0.1/windows\"><img src=\"/gifs/windows-icon.png\" />DLL</a></td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=4.0.1\">Changelog</a>]</small></td>\n" + " </tr>\n" + " <tr>\n" + " <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/4.0.0\">4.0.0</a></th>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">beta</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\">2013-03-26</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-4.0.0.tgz\">apcu-4.0.0.tgz</a> (101.8kB)</td>\n" + " <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=4.0.0\">Changelog</a>]</small></td>\n" + " </tr>\n" + " </table>"; final Pattern pattern = Pattern.compile(regex); final Matcher matcher = pattern.matcher(string); if (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