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'''
'''
gmxi

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 = "(?#First, match the protocol)\n" + "(?:https?|ftp)://\n" + "(?#Next, check for optional username and/or password)\n" + "(?#Note: The following two char classes are functionally equivalent)\n" + "(?:[\\x21-\\x39\\x3b-\\x3f\\x41-\\x7e]+(?::[!-9;-?A-~]+)?@)?\n" + "(?#Next, let's match the domain [with support for Punycode ])\n" + "(?:xn--[0-9a-z]+|[0-9A-Za-z_-]+\\.)*(?:xn--[0-9a-z]+|[0-9A-Za-z-]+)\\.(?:xn--[0-9a-z]+|[0-9A-Za-z]{2,10})\n" + "(?#Let's match on optional port)\n" + "(?::(?:6553[0-5]|655[0-2]\\d|65[0-4]\\d{2}|6[0-4]\\d{3}|[1-5]\\d{4}|[1-9]\\d{1,3}|\\d))?\n" + "(?#Next, let's match on the path)\n" + "(?:/[\\x21\\x22\\x24\\x25\\x27-x2e\\x30-\\x3b\\x3e\\x40-\\x5b\\x5d-\\x7e]*)*\n" + "(?#Next, let's match on an anchor)\n" + "(?:\\#[\\x21\\x22\\x24\\x25\\x27-x2e\\x30-\\x3b\\x3e\\x40-\\x5b\\x5d-\\x7e]*)?\n" + "(?#Last, but not least, we match on URI params)\n" + "(?:\\?[\\x21\\x22\\x24\\x25\\x27-\\x2e\\x30-\\x3b\\x40-\\x5b\\x5d-\\x7e]+=[\\x21\\x22\\x24\\x25\\x27-\\x2e\\x30-\\x3b\\x40-\\x5b\\x5d-\\x7e]*)?\n" + "(?#Additional params)\n" + "(?:&[\\x21\\x22\\x24\\x25\\x27-\\x2e\\x30-\\x3b\\x40-\\x5b\\x5d-\\x7e]+=[\\x21\\x22\\x24\\x25\\x27-\\x2e\\x30-\\x3b\\x40-\\x5b\\x5d-\\x7e]*)*\n"; final String string = "https://thechildrenareourfuture.org/#SomeRandomAnchorGoesHere\n" + "http://user:pass@example.com:8080/omega:33\n" + "https://www.google.com:65535/\n" + "https://www.google.com:42069/\n" + "https://www.google.com:9876/\n" + "https://www.google.com:59999/\n" + "https://www.google.com:10000/\n" + "https://www.google.com:1234/\n" + "https://www.google.com:53/\n" + "https://www.google.com:123/\n" + "https://www.google.com:0/\n" + "ftp://username:password@example.com:21/path/here/to/file.tar.gz\n" + "http://google.com?redirect=https%3A%2F%2Flocalhost%3A8080&var2=somethingelse\n" + "https://john.smith@github.com/UserGroup/repo.git\n" + "https://mattermost.com:8065/team/messages/@john.smith\n" + "https://punycode.xn--j6w193g\n\n" + "smtp://somesite.com/myFile.php\n" + "SMTP://SOMESITE.COM/MYFILE.PHP\n" + "unknown://somesite.com/fiLeNuM12345.php\n" + "//SomeSite.ru/foO.php\n" + "://SomeOtherSite.oRg/baRR.php\n\n" + "https://_sub1.sub2.sub3.s-u_b4.domain.com\n\n" + "http://username:password@subdomain.domain.co.uk/path/to/file.html#Anchor1?key1=value1&key2=value2"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.COMMENTS | 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