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

/
/
gmi

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-zA-Z){2}\\/)?(app|album|book|audiobook|author|movie|movie-collection|studio|artist|developer|music-video|video|podcast|tv-season|tv-show)\\/((a-zA-Z0-9\\-)*\\/)?(id)?(0-9)*"; final String string = "//bad link examples from past traffic:\n" + "/bkp.sql.tar.gz\n" + "/bkp.sql.tar.gz\n" + "/core/modules/system/tests/modules/error_service_test/src/Logger\n" + "/_get/spool/kjax/rrmjaxfl.923e30d17ca8f0b37a489c818eae0577.txt.js\n" + "/_get/spool/kjax/rrmjaxfl.923e30d17ca8f0b37a489c818eae0577.txt.php\n" + "/?AT=10l9IM&i=469623817&uo=4/cat ../../../../../../../../etc/passwd \n" + "/?at=11lK2U/..///////..////..//////etc/passwd&forcetoken=11lK2U/..///////..////..//////etc/passwd \n\n" + "//can't do anything with these, no point in calling us\n" + "/subscribe?app=music&at=1001lLm9&itscg=30200&itsct=addotnet&ct=884-11165151 \n" + "/lookup?id=622463230\n" + "/search?term=You're%20Not%20There%20-%20Lukas%20Graham&entity=song&media=music&country=DK&limit=1\n" + "/WebObjects/MZSearch.woa/wa/advancedSearch?s=143443&partnerId=2003&affToken=www.last.fm&at=10l3Sh&artistTerm=Schmetterlinge&songTerm=Wer+schreibt+die+Geschichte?\n" + "/actuator/jolokia\n\n" + "//supported by GeniusLink\n" + "/app/id1235\n" + "/us/app/id1235\n" + "/us/app/among-us/id1351168404\n" + "/us/app/1password-7-password-manager/id1333542190?mt=12\n" + "/us/book/dune/id597944491\n" + "/us/audiobook/a-promised-land-unabridged/id1540585069\n" + "/us/author/c.j.-archer/id421351544?mt=11\n" + "/us/movie/tenet/id1538251548\n" + "/us/movie-collection/the-lord-of-the-rings-extended-editions-bundle/id1537502630\n" + "/us/studio/pixar/id81758682\n" + "/us/album/willow/1544268281?i=1544268298\n" + "/us/album/evermore/1544268281\n" + "/us/artist/taylor-swift/159260351\n" + "/us/music-video/i-dont-care-bonus-video/1445881948\n" + "/au/video/id254295715?mt=5\n" + "/us/podcast/the-chernobyl-podcast/id1459712981\n" + "/us/tv-season/freight-trains-and-monsters/id1514426968?i=1520794309\n" + "/us/tv-season/yellowstone-season-3/id1514426968\n" + "/ca/tv-show/come-fly-with-me/id410581843\n" + "/us/developer/tapbots/id293642940\n" + "/video/histories/id253624330?uo=5&AT=10l9IM\n" + "/video/course-you-know-this-means/id665084512?uo=5&AT=10l9IM \n" + "/artist/jaime-urrutia/id27431058?uo=5&at=1010l34Ub&ct=cart&app=music\n" + "/app/noteshelf-2/id1271086060?mt=8&uo=4&at=11l9z8\n" + "/album/1552222070?uo=4&app=itunes&at=1l3vpUI&lId=22814830&cId=none&sr=6&src=Linkfire&itscg=30440&itsct=catchall_p6&ct=LFV_44d9b6dc091636bfbe66b473b4135330&ls=1\n\n\n"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); 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