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

/
/
gim

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 = "(?<FULLREQUEST>^(?<HOST>(?<PROTOCOL>(?:http)?s?:?\\/\\/)(?<DOMAIN>[^\\/]+\\/)|(?<RELATIVE>(?:\\.*\\/?)+))(?<PATH>[^\\s]+\\/)?(?<FILENAME>(?:(?<NAME>[^\\/\\s\\.\\?\\#]+?)?(?<EXT>\\.[^\\s\\?\\#]*)?|(?:\\/[^\\/\\s\\?\\#]+?)))(?<QUERYSTRING>\\?[^\\s\\#]+)?(?<ANCHOR>\\#\\S+)?$)"; final String string = "Domain Name Root\n" + "https://www.domain.com/\n" + "https://www.domain.com/?with=queryString\n" + "https://www.domain.com/#withAnchor\n" + "https://www.domain.com/?with=queryString#andAnchor\n\n" + "Full Domain Name With File\n" + "https://www.domain.com/with-file\n" + "https://www.domain.com/with-file?with=queryString\n" + "https://www.domain.com/with-file#withAnchor\n" + "https://www.domain.com/with-file?with=queryString#andAnchor\n\n" + "Full Domain Name With File With Extension\n" + "https://www.domain.com/file-with.ext\n" + "https://www.domain.com/file-with.ext?with=queryString\n" + "https://www.domain.com/file-with.ext#withAnchor\n" + "https://www.domain.com/file-with.ext?with=queryString#andAnchor\n\n" + "Full Domain Name With Path to File\n" + "https://www.domain.com/some-path/to/a/file\n" + "https://www.domain.com/some-path/to/a/file?with=queryString\n" + "https://www.domain.com/some-path/to/a/file#withAnchor\n" + "https://www.domain.com/some-path/to/a/file?with=queryString#andAnchor\n\n" + "Full Domain Name With Path to File With Extension\n" + "https://www.domain.com/some-path/to/a/file-with.ext\n" + "https://www.domain.com/some-path/to/a/file-with.ext?with=queryString\n" + "https://www.domain.com/some-path/to/a/file-with.ext\n" + "https://www.domain.com/some-path/to/a/file-with.ext?with=queryString#andAnchor\n\n" + "Dynamic Protocol Full Domain\n" + "//www.domain.com/\n" + "//www.domain.com/?with=queryString\n" + "//www.domain.com/#withAnchor\n" + "//www.domain.com/?with=queryString#andAnchor\n\n" + "Dynamic Protocol (Full Domain) to File\n" + "//www.domain.com/with-file\n" + "//www.domain.com/with-file?with=queryString\n" + "//www.domain.com/with-file#withAnchor\n" + "//www.domain.com/with-file?with=queryString#andAnchor\n\n" + "Dynamic Protocol (Full Domain) to File With Extension\n" + "//www.domain.com/with-file-with.ext\n" + "//www.domain.com/with-file-with.ext?with=queryString\n" + "//www.domain.com/with-file-with.ext#withAnchor\n" + "//www.domain.com/with-file-with.ext?with=queryString#andAnchor\n\n" + "Dynamic Protocol (Full Domain), Path Leading to File\n" + "//www.domain.com/with/path/to/file\n" + "//www.domain.com/with/path/to/file?with=queryString\n" + "//www.domain.com/with/path/to/file#withAnchor\n" + "//www.domain.com/with/path/to/file?with=queryString#andAnchor\n\n" + "Dynamic Protocol (Full Domain), Path Leading to File With Extension\n" + "//www.domain.com/with/path/to/file-with.ext\n" + "//www.domain.com/with/path/to/file-with.ext?with=queryString\n" + "//www.domain.com/with/path/to/file-with.ext#withAnchor\n" + "//www.domain.com/with/path/to/file-with.ext?with=queryString#andAnchor\n" + "Root Relative File\n" + "/some-root-file\n" + "/some-root-file?with=queryString\n" + "/some-root-file#withAnchor\n" + "/some-root-file?with=queryString#andAnchor\n\n" + "Root Relative File With Extension\n" + "/root-file-with.ext\n" + "/root-file-with.ext?with=queryString\n" + "/root-file-with.ext#withAnchor\n" + "/root-file-with.ext?with=queryString#andAnchor\n\n" + "Root Path to File\n" + "/some-root-path/to/a/file\n" + "/some-root-path/to/a/file?with=queryString\n" + "/some-root-path/to/a/file#withAnchor\n" + "/some-root-path/to/a/file?with=queryString#andAnchor\n\n" + "Root Path to File With Extension\n" + "/some-root-path/to/a/file-with.ext\n" + "/some-root-path/to/a/file-with.ext?with=queryString\n" + "/some-root-path/to/a/file-with.ext#withAnchor\n" + "/some-root-path/to/a/file-with.ext?with=queryString#andAnchor\n\n" + "Relative (Same As Requestor) File\n" + "./relative-to-requester-file\n" + "./relative-to-requester-file?with=queryString\n" + "./relative-to-requester-file#withAnchor\n" + "./relative-to-requester-file?with=queryString#andAnchor\n\n" + "Relative (Same As Requestor) File With Extension\n" + "./relative-to-requester-file-with.ext\n" + "./relative-to-requester-file-with.ext?with=queryString\n" + "./relative-to-requester-file-with.ext#withAnchor\n" + "./relative-to-requester-file-with.ext?with=queryString#andAnchor\n\n" + "Relative (Same As Requestor) Path To File\n" + "./relative-to-requester-path/to/a/file\n" + "./relative-to-requester-path/to/a/file?with=queryString\n" + "./relative-to-requester-path/to/a/file#withAnchor\n" + "./relative-to-requester-path/to/a/file?with=queryString#andAnchor\n\n" + "Relative (Same As Requestor) Path To File With Extension\n" + "./relative-to-requester-path/to/a/file-with.ext\n" + "./relative-to-requester-path/to/a/file-with.ext?with=queryString\n" + "./relative-to-requester-path/to/a/file-with.ext#withAnchor\n" + "./relative-to-requester-path/to/a/file-with.ext?with=queryString#andAnchor\n\n" + "Relative (Above Requestor) File\n" + "../relative-file-up\n" + "../relative-file-up?with=queryString\n" + "../relative-file-up#withAnchor\n" + "../relative-file-up?with=queryString#andAnchor\n\n" + "Relative (Above Requestor) File With Extension\n" + "../relative-file-up-with.ext\n" + "../relative-file-up-with.ext?with=queryString\n" + "../relative-file-up-with.ext#withAnchor\n" + "../relative-file-up-with.ext?with=queryString#andAnchor\n\n" + "Relative (two Above Requestor) File\n" + "../../relative-file-two-up\n" + "../../relative-file-two-up?with=queryString\n" + "../../relative-file-two-up#withAnchor\n" + "../../relative-file-two-up?with=queryString#andAnchor\n\n" + "Relative (two Above Requestor) File With Extension\n" + "../../relative-file-two-up.ext\n" + "../../relative-file-two-up.ext?with=queryString\n" + "../../relative-file-two-up.ext#withAnchor\n" + "../../relative-file-two-up.ext?with=queryString#andAnchor\n\n" + "Relative (n Above Requestor) File\n" + "../../../../../../../../relative-file-n-up\n" + "../../../../../../../../relative-file-n-up?with=queryString\n" + "../../../../../../../../relative-file-n-up#withAnchor\n" + "../../../../../../../../relative-file-n-up?with=queryString#andAnchor\n\n" + "Relative Path (Above Requestor) to File\n" + "../relative-path/up/to/a/file\n" + "../relative-path/up/to/a/file?with=queryString\n" + "../relative-path/up/to/a/file#withAnchor\n" + "../relative-path/up/to/a/file?with=queryString#andAnchor\n\n" + "Relative Path (Above Requestor) to File With Extension\n" + "../relative-path/up/to/a/file-with.ext\n" + "../relative-path/up/to/a/file-with.ext?with=queryString\n" + "../relative-path/up/to/a/file-with.ext#withAnchor\n" + "../relative-path/up/to/a/file-with.ext?with=queryString#andAnchor\n\n" + "Relative Path (Two Above Requestor) to File\n" + "../../relative-path/two/up/to/a/file\n" + "../../relative-path/two/up/to/a/file?with=queryString\n" + "../../relative-path/two/up/to/a/file#withAnchor\n" + "../../relative-path/two/up/to/a/file?with=queryString#andAnchor\n\n" + "Relative Path (Two Above Requestor) to File With Extension\n" + "../../relative-path/two/up/to/a/file-with.ext\n" + "../../relative-path/two/up/to/a/file-with.ext?with=queryString\n" + "../../relative-path/two/up/to/a/file-with.ext#withAnchor\n" + "../../relative-path/two/up/to/a/file-with.ext?with=queryString#andAnchor\n\n" + "Relative Path (n Above Requestor) to File\n" + "../../../../../relative-path/n/up/to/a/file\n" + "../../../../../relative-path/n/up/to/a/file?with=queryString\n" + "../../../../../relative-path/n/up/to/a/file#withAnchor\n" + "../../../../../relative-path/n/up/to/a/file?with=queryString#andAnchor\n\n" + "Relative Path (n Above Requestor) to File With Extension\n" + "../../../../../relative-path/n/up/to/a/file-with.ext\n" + "../../../../../relative-path/n/up/to/a/file-with.ext?with=queryString\n" + "../../../../../relative-path/n/up/to/a/file-with.ext#withAnchor\n" + "../../../../../relative-path/n/up/to/a/file-with.ext?with=queryString#andAnchor\n"; final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | 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