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

/
/
im

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 = "<a (.*?)test(.*?)<\\/a>"; final String string = "<a style=\"text-decoration: none;\" target=\"_blank\" href=\"http://example.com/?qs=df8fc6df1b3b6e7b87c5c8da8e2c0ad34b73a19751c15d1b3efbf37c3ded58b9cca09ee6d80e64bc\"><img width=\"120\" height=\"81\" border=\"0\" style=\"display: block; font-family: Arial,Helvetica,sans-serif; font-size: 14px; color: #004990;\" title=\"example.com\" alt=\"example.com\" src=\"http://example.com/lib/fe8f12717d67017f72/m/5/20140513_example_Logo1.jpg\" /></a></td> </tr> <tr> <td valign=\"top\" align=\"left\"> <table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"> <tbody> <tr> <td valign=\"top\" height=\"17\" align=\"left\" style=\"font-size: 0%;\"><img width=\"1\" height=\"17\" border=\"0\" style=\"display: block;\" title=\"\" alt=\"\" src=\"http://example.com/lib/fe8f12717d62057f7d/m/1/img_spacer.gif\" /></td> </tr> <tr> <td valign=\"top\" align=\"left\" style=\"font-family: Arial,Helvetica,sans-serif; font-size: 11px; color: #656c73; line-height: 18px;\">You are subscribed as <a reportname=\"mailto:_EMAIL__\" style=\"text-decoration: none; color: #505050;\" href=\"example@example.com\"><span class=\"link3\" style=\"text-decoration: none;\">example@example.com</span></a></td> </tr> <tr> <td valign=\"top\" height=\"25\" align=\"left\" style=\"font-size: 0%;\"><img width=\"1\" height=\"25\" border=\"0\" style=\"display: block;\" title=\"\" alt=\"\" src=\"http://example.com\" /></td> </tr> <tr> <td valign=\"top\" align=\"left\" style=\"font-family: Arial,Helvetica,sans-serif; font-size: 11px; color: #656c73; line-height: 18px;\">View and manage your <span class=\"applefooterlinks\">example.com </span><a style=\"text-decoration: underline; color: #656c73;\" target=\"_blank\" href=\"http://example.com/\"><span class=\"link3\" style=\"color: #656c73; text-decoration: underline;\">newsletter preferences</span></a>.<br /> If you wish to unsubscribe you must uncheck the box next to the relevant title.</td> </tr> <tr> <td valign=\"top\" height=\"25\" align=\"left\" style=\"font-size: 0%;\"><img width=\"1\" height=\"25\" border=\"0\" style=\"display: block;\" title=\"\" alt=\"\" src=\"http://example.com\" /></td> </tr> <tr> <td valign=\"top\" align=\"left\" style=\"font-family: Arial,Helvetica,sans-serif; font-size: 11px; color: #656c73; line-height: 18px;\"><a style=\"text-decoration: underline; color: #656c73;\" target=\"_blank\" href=\"http://example.com/?qs=85ea4dcfb406c17e184090f0720216d008d21d0363ca86741a296ce98649846e5262048f59eea25e\"><span class=\"link3\" style=\"color: #656c73; text-decoration: underline;\">TEST</span></a>"; final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); 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