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

/
/
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 = "<div[\\s\\S]*?id=\\\"variationsTwister[\\s\\S]*?<span[\\s\\S]*?a-dropdown-container[\\s\\S]*?<select[\\s\\S]*?<\\/select>[\\s\\S]*?Color:<\\/span>[\\s\\S]*?<span[\\s\\S]*?>([\\s\\S]*?)<\\/span>"; final String string = "<div id=\"variationsTwister\" class=\"a-section a-spacing-mini\">\n" + " \n\n" + " \n\n\n\n\n\n\n\n\n" + " \n" + " \n\n" + " <div class=\"a-section a-spacing-micro\">\n" + " <span class=\"a-size-base\">Size:</span>\n" + " <span class=\"a-size-base a-text-bold\"></span>\n" + " </div>\n" + " <span class=\"a-dropdown-container\"><select name=\"\" autocomplete=\"off\" data-a-native-class=\"olpTwisterDropdown\" tabindex=\"-1\" class=\"a-native-dropdown a-spacing-small olpTwisterDropdown\">\n" + " <option value=\"/gp/offer-listing/B00NNA39IE/ref=olp_twister_all?ie=UTF8&amp;mv_color_name=3&amp;mv_size_name=all\">\n" + " Lowest offer for each\n" + " </option>\n" + " <option value=\"/gp/offer-listing/B00MC3QSKO/ref=olp_twister_child?ie=UTF8&amp;mv_color_name=3&amp;mv_size_name=0\">\n" + " 5 M US\n" + " </option>\n" + " <option value=\"/gp/offer-listing/B00MC3QU1G/ref=olp_twister_child?ie=UTF8&amp;mv_color_name=3&amp;mv_size_name=1\">\n" + " 5.5 M US\n" + " </option>\n" + " <option value=\"/gp/offer-listing/B00MC3QX72/ref=olp_twister_child?ie=UTF8&amp;mv_color_name=3&amp;mv_size_name=2\">\n" + " 6 M US\n" + " </option>\n" + " <option value=\"/gp/offer-listing/B00MC3QYQC/ref=olp_twister_child?ie=UTF8&amp;mv_color_name=3&amp;mv_size_name=3\">\n" + " 6.5 M US\n" + " </option>\n" + " <option value=\"/gp/offer-listing/B00MC3R0PQ/ref=olp_twister_child?ie=UTF8&amp;mv_color_name=3&amp;mv_size_name=4\">\n" + " 7 M US\n" + " </option>\n" + " <option value=\"/gp/offer-listing/B00MC3R23Q/ref=olp_twister_child?ie=UTF8&amp;mv_color_name=3&amp;mv_size_name=5\">\n" + " 7.5 M US\n" + " </option>\n" + " <option value=\"/gp/offer-listing/B00MC3R3M6/ref=olp_twister_child?ie=UTF8&amp;mv_color_name=3&amp;mv_size_name=6\" selected=\"\">\n" + " 8 M US\n" + " </option>\n" + " <option value=\"/gp/offer-listing/B00MC3R6LO/ref=olp_twister_child?ie=UTF8&amp;mv_color_name=3&amp;mv_size_name=7\">\n" + " 8.5 M US\n" + " </option>\n" + " <option value=\"/gp/offer-listing/B00MC3R9IY/ref=olp_twister_child?ie=UTF8&amp;mv_color_name=3&amp;mv_size_name=8\">\n" + " 9 M US\n" + " </option>\n" + " <option value=\"/gp/offer-listing/B00MC3RBE6/ref=olp_twister_child?ie=UTF8&amp;mv_color_name=3&amp;mv_size_name=9\">\n" + " 9.5 M US\n" + " </option>\n" + " <option value=\"/gp/offer-listing/B00MC3RCPY/ref=olp_twister_child?ie=UTF8&amp;mv_color_name=3&amp;mv_size_name=10\">\n" + " 10 M US\n" + " </option>\n" + " </select><span tabindex=\"-1\" id=\"olpTwisterDropdownAUI\" class=\"a-button a-button-dropdown a-button-small a-spacing-small\" style=\"min-width: 0.583333%;\"><span class=\"a-button-inner\"><span class=\"a-button-text a-declarative\" data-action=\"a-dropdown-button\" role=\"button\" tabindex=\"0\" aria-hidden=\"true\"><span class=\"a-dropdown-prompt\">\n" + " 8 M US\n" + " </span></span><i class=\"a-icon a-icon-dropdown\"></i></span></span></span>\n" + " <div class=\"a-section a-spacing-micro\">\n" + " <span class=\"a-size-base\">Color:</span>\n" + " <span class=\"a-size-base a-text-bold\">Taupe</span>\n" + " </div>\n" + " <ul class=\"a-unordered-list a-nostyle a-button-list a-declarative a-button-toggle-group a-horizontal a-spacing-small\" role=\"radiogroup\" data-action=\"a-button-group\">\n" + " <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n" + " <span class=\"a-button a-button-normal a-button-thumbnail a-button-toggle\" id=\"a-autoid-0\"><span class=\"a-button-inner\"><a href=\"/gp/offer-listing/B0002MB0DM/ref=olp_twister_child?ie=UTF8&amp;mv_color_name=0&amp;mv_size_name=6\" class=\"a-button-text\" role=\"button\" id=\"a-autoid-0-announce\">\n" + " <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/41jl1EwZjHL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n" + " </a></span></span>\n" + " </span></li>\n" + " <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n" + " <span class=\"a-button a-button-normal a-button-thumbnail a-button-toggle\" id=\"a-autoid-1\"><span class=\"a-button-inner\"><a href=\"/gp/offer-listing/B005BFV1DO/ref=olp_twister_child?ie=UTF8&amp;mv_color_name=1&amp;mv_size_name=6\" class=\"a-button-text\" role=\"button\" id=\"a-autoid-1-announce\">\n" + " <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/41XqEgaqwtL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n" + " </a></span></span>\n" + " </span></li>\n" + " <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n" + " <span class=\"a-button a-button-normal a-button-thumbnail a-button-toggle\" id=\"a-autoid-2\"><span class=\"a-button-inner\"><a href=\"/gp/offer-listing/B00MC3SFXW/ref=olp_twister_child?ie=UTF8&amp;mv_color_name=2&amp;mv_size_name=6\" class=\"a-button-text\" role=\"button\" id=\"a-autoid-2-announce\">\n" + " <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/41cNhnckTFL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n" + " </a></span></span>\n" + " </span></li>\n" + " <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n" + " <span class=\"a-button a-button-selected a-button-thumbnail a-button-toggle\" id=\"a-autoid-3\"><span class=\"a-button-inner\"><a href=\"/gp/offer-listing/B00MC3R3M6/ref=olp_twister_child?ie=UTF8&amp;mv_color_name=3&amp;mv_size_name=6\" class=\"a-button-text\" role=\"button\" id=\"a-autoid-3-announce\">\n" + " <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/41p2f8xA8TL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n" + " </a></span></span>\n" + " </span></li>\n" + " <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n" + " <span class=\"a-button a-button-normal a-button-thumbnail a-button-toggle\" id=\"a-autoid-4\"><span class=\"a-button-inner\"><a href=\"/gp/offer-listing/B011SBR9PS/ref=olp_twister_child?ie=UTF8&amp;mv_color_name=4&amp;mv_size_name=6\" class=\"a-button-text\" role=\"button\" id=\"a-autoid-4-announce\">\n" + " <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/311Xw94d26L._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n" + " </a></span></span>\n" + " </span></li>\n" + " <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n" + " <span class=\"a-button a-button-normal a-button-thumbnail a-button-toggle\" id=\"a-autoid-5\"><span class=\"a-button-inner\"><a href=\"/gp/offer-listing/B07FYS34S4/ref=olp_twister_child?ie=UTF8&amp;mv_color_name=5&amp;mv_size_name=6\" class=\"a-button-text\" role=\"button\" id=\"a-autoid-5-announce\">\n" + " <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/41r4qvtmEkL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n" + " </a></span></span>\n" + " </span></li>\n" + " <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n" + " <span class=\"a-button a-button-normal a-button-thumbnail a-button-toggle\" id=\"a-autoid-6\"><span class=\"a-button-inner\"><a href=\"/gp/offer-listing/B00MC3TVSK/ref=olp_twister_child?ie=UTF8&amp;mv_color_name=6&amp;mv_size_name=6\" class=\"a-button-text\" role=\"button\" id=\"a-autoid-6-announce\">\n" + " <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/31YEDbIcNwL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n" + " </a></span></span>\n" + " </span></li>\n" + " <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n" + " <span class=\"a-button a-button-normal a-button-thumbnail a-button-toggle\" id=\"a-autoid-7\"><span class=\"a-button-inner\"><a href=\"/gp/offer-listing/B016W1Z1RC/ref=olp_twister_child?ie=UTF8&amp;mv_color_name=7&amp;mv_size_name=6\" class=\"a-button-text\" role=\"button\" id=\"a-autoid-7-announce\">\n" + " <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/41plM8TSFBL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n" + " </a></span></span>\n" + " </span></li>\n" + " <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n" + " <span class=\"a-button a-button-normal a-button-thumbnail a-button-toggle\" id=\"a-autoid-8\"><span class=\"a-button-inner\"><a href=\"/gp/offer-listing/B0007L7WPS/ref=olp_twister_child?ie=UTF8&amp;mv_color_name=8&amp;mv_size_name=6\" class=\"a-button-text\" role=\"button\" id=\"a-autoid-8-announce\">\n" + " <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/41NGM8gp7hL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n" + " </a></span></span>\n" + " </span></li>\n" + " <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n" + " <span class=\"a-button a-button-disabled a-button-thumbnail a-button-toggle\" id=\"a-autoid-9\"><span class=\"a-button-inner\"><input disabled=\"disabled\" class=\"a-button-input\" type=\"submit\" aria-labelledby=\"a-autoid-9-announce\"><span class=\"a-button-text\" aria-hidden=\"true\" id=\"a-autoid-9-announce\">\n" + " <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/41WBKxWvoLL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n" + " </span></span></span>\n" + " </span></li>\n" + " <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n" + " <span class=\"a-button a-button-disabled a-button-thumbnail a-button-toggle\" id=\"a-autoid-10\"><span class=\"a-button-inner\"><input disabled=\"disabled\" class=\"a-button-input\" type=\"submit\" aria-labelledby=\"a-autoid-10-announce\"><span class=\"a-button-text\" aria-hidden=\"true\" id=\"a-autoid-10-announce\">\n" + " <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/41ap6ahxMRL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n" + " </span></span></span>\n" + " </span></li>\n" + " <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n" + " <span class=\"a-button a-button-disabled a-button-thumbnail a-button-toggle\" id=\"a-autoid-11\"><span class=\"a-button-inner\"><input disabled=\"disabled\" class=\"a-button-input\" type=\"submit\" aria-labelledby=\"a-autoid-11-announce\"><span class=\"a-button-text\" aria-hidden=\"true\" id=\"a-autoid-11-announce\">\n" + " <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/41QD5K54hPL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n" + " </span></span></span>\n" + " </span></li>\n" + " <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n" + " <span class=\"a-button a-button-disabled a-button-thumbnail a-button-toggle\" id=\"a-autoid-12\"><span class=\"a-button-inner\"><input disabled=\"disabled\" class=\"a-button-input\" type=\"submit\" aria-labelledby=\"a-autoid-12-announce\"><span class=\"a-button-text\" aria-hidden=\"true\" id=\"a-autoid-12-announce\">\n" + " <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/31qtQ78OWiL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n" + " </span></span></span>\n" + " </span></li>\n" + " <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n" + " <span class=\"a-button a-button-normal a-button-toggle\" id=\"a-autoid-13\"><span class=\"a-button-inner\"><a href=\"/gp/offer-listing/B00NNA39IE/ref=olp_twister_all?ie=UTF8&amp;mv_color_name=all&amp;mv_size_name=6\" class=\"a-button-text\" role=\"button\" id=\"a-autoid-13-announce\">\n" + " Lowest offer for each\n" + " </a></span></span>\n" + " </span></li>\n" + " </ul>\n\n" + " </div>"; 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