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 = "^(<\\!\\-\\- wp:onecms\\/recipe-time \\{\\\"type\\\"\\:\\\"cook\\\")\\S(.*[a-z\\:0-9\\}\\s])(\\/\\-\\->)$"; final String string = "<!-- wp:onecms/recipe-header {\"dek\":\"\\u003cp\\u003eHungry for some \\u003ca href=\\u0022https://www.southernliving.com/recipes/old-fashioned-apple-pie\\u0022\\u003eapple pie\\u003c/a\\u003e? We are right there with you. Here's the recipe from the Whispering Canyon Café at Disney’s Wilderness Lodge—dessert will never be the same.\\u003c/p\\u003e\"} /-->\n\n" + "<!-- wp:onecms/primary-media /-->\n\n" + "<!-- wp:onecms/recipe-media /-->\n\n" + "<!-- wp:onecms/recipe-container -->\n" + "<!-- wp:onecms/recipe-details -->\n" + "<!-- wp:onecms/recipe-metadata {\"yieldDescription\":\"Makes one 9-inch pie.\"} /-->\n\n" + "<!-- wp:onecms/recipe-times -->\n" + "<!-- wp:onecms/recipe-time {\"type\":\"hands-on\",\"days\":0,\"hours\":0,\"minutes\":45} /-->\n\n" + "<!-- wp:onecms/recipe-time {\"type\":\"cook\",\"days\":0,\"hours\":1,\"minutes\":0} /-->\n" + "<!-- /wp:onecms/recipe-times -->\n" + "<!-- /wp:onecms/recipe-details -->\n\n" + "<!-- wp:onecms/recipe-ingredients -->\n" + "<!-- wp:onecms/recipe-section-header {\"content\":\"Pie Crust\"} /-->\n\n" + "<!-- wp:onecms/recipe-ingredient-custom {\"customIngredient\":\"butter, cut into small pieces\",\"customMeasure\":\"tablespoons\",\"customQuantity\":\"4\"} /-->\n\n" + "<!-- wp:onecms/recipe-ingredient-custom {\"customIngredient\":\"shortening\",\"customMeasure\":\"cup\",\"customQuantity\":\"0.25\"} /-->\n\n" + "<!-- wp:onecms/recipe-ingredient-custom {\"customIngredient\":\"all-purpose flour\",\"customMeasure\":\"cups\",\"customQuantity\":\"1.75\"} /-->\n\n" + "<!-- wp:onecms/recipe-ingredient-custom {\"customIngredient\":\"sugar\",\"customMeasure\":\"teaspoons\",\"customQuantity\":\"4\"} /-->\n\n" + "<!-- wp:onecms/recipe-ingredient-custom {\"customIngredient\":\"coarse salt\",\"customMeasure\":\"teaspoon\",\"customQuantity\":\"0.13\"} /-->\n\n" + "<!-- wp:onecms/recipe-ingredient-custom {\"customIngredient\":\"2% milk\",\"customMeasure\":\"cup\",\"customQuantity\":\"0.33\"} /-->\n\n" + "<!-- wp:onecms/recipe-section-header {\"content\":\"Apples\"} /-->\n\n" + "<!-- wp:onecms/recipe-ingredient-custom {\"customIngredient\":\"water\",\"customMeasure\":\"cup\",\"customQuantity\":\"1\"} /-->\n\n" + "<!-- wp:onecms/recipe-ingredient-custom {\"customIngredient\":\"Granny Smith apples, peeled and sliced\",\"customQuantity\":\"6\"} /-->\n\n" + "<!-- wp:onecms/recipe-ingredient-custom {\"customIngredient\":\"apple pie spice\",\"customMeasure\":\"teaspoon\",\"customQuantity\":\"2\"} /-->\n\n" + "<!-- wp:onecms/recipe-section-header {\"content\":\"Apple Pie Batter\"} /-->\n\n" + "<!-- wp:onecms/recipe-ingredient-custom {\"customIngredient\":\"butter, softened\",\"customMeasure\":\"cup\",\"customQuantity\":\"0.5\"} /-->\n\n" + "<!-- wp:onecms/recipe-ingredient-custom {\"customIngredient\":\"sugar\",\"customMeasure\":\"cup\",\"customQuantity\":\"0.67\"} /-->\n\n" + "<!-- wp:onecms/recipe-ingredient-custom {\"customIngredient\":\"eggs\",\"customQuantity\":\"2\"} /-->\n\n" + "<!-- wp:onecms/recipe-ingredient-custom {\"customIngredient\":\"flour\",\"customMeasure\":\"cups\",\"customQuantity\":\"1.5\"} /-->\n\n" + "<!-- wp:onecms/recipe-ingredient-custom {\"customIngredient\":\"baking powder\",\"customMeasure\":\"teaspoon\",\"customQuantity\":\"1.5\"} /-->\n\n" + "<!-- wp:onecms/recipe-ingredient-custom {\"customIngredient\":\"salt\",\"customMeasure\":\"teaspoon\",\"customQuantity\":\"0.13\"} /-->\n\n" + "<!-- wp:onecms/recipe-ingredient-custom {\"customIngredient\":\"heavy cream\",\"customMeasure\":\"cup\",\"customQuantity\":\"0.25\"} /-->\n" + "<!-- /wp:onecms/recipe-ingredients -->\n\n" + "<!-- wp:onecms/recipe-nutrition {\"displayNutritionRecipe\":false} /-->\n\n" + "<!-- wp:onecms/recipe-directions -->\n" + "<!-- wp:onecms/recipe-direction {\"directionId\":\"ddaa1eec-7bec-4284-b4e3-53b35cb83941\",\"content\":\"\\u003cstrong\\u003eFOR PIE CRUST: \\u003c/strong\\u003e\"} /-->\n\n" + "<!-- wp:onecms/recipe-direction {\"directionId\":\"e546e3c3-96a5-4be5-94fa-34ee625f3a48\",\"content\":\"\\u003cstrong\\u003eFOR APPLES:\\u003c/strong\\u003e\"} /-->\n\n" + "<!-- wp:onecms/recipe-direction {\"directionId\":\"49fbc0c6-12f0-420c-9dc6-19210620e023\",\"content\":\"\\u003cstrong\\u003eFOR APPLE PIE BATTER:\\u003c/strong\\u003e\"} /-->\n\n" + "<!-- wp:onecms/recipe-direction {\"directionId\":\"208717df-e3e3-43e9-a2bc-35ad854ae43c\",\"content\":\"\\u003cstrong\\u003eFOR APPLE PIE:\\u003c/strong\\u003e\"} /-->\n" + "<!-- /wp:onecms/recipe-directions -->\n\n" + "<!-- wp:onecms/recipe-notes -->\n" + "<!-- wp:onecms/recipe-note /-->\n" + "<!-- /wp:onecms/recipe-notes -->\n" + "<!-- /wp:onecms/recipe-container -->"; 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