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

using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string pattern = @"(?<FULLREQUEST>^(?<HOST>(?<PROTOCOL>(?:http)?s?:?\/\/)(?<DOMAIN>[^\/]+\/)|(?<RELATIVE>(?:\.*\/?)+))(?<PATH>[^\s]+\/)?(?<FILENAME>(?:(?<NAME>[^\/\s\.\?\#]+?)?(?<EXT>\.[^\s\?\#]*)?|(?:\/[^\/\s\?\#]+?)))(?<QUERYSTRING>\?[^\s\#]+)?(?<ANCHOR>\#\S+)?$)"; string input = @"Domain Name Root https://www.domain.com/ https://www.domain.com/?with=queryString https://www.domain.com/#withAnchor https://www.domain.com/?with=queryString#andAnchor Full Domain Name With File https://www.domain.com/with-file https://www.domain.com/with-file?with=queryString https://www.domain.com/with-file#withAnchor https://www.domain.com/with-file?with=queryString#andAnchor Full Domain Name With File With Extension https://www.domain.com/file-with.ext https://www.domain.com/file-with.ext?with=queryString https://www.domain.com/file-with.ext#withAnchor https://www.domain.com/file-with.ext?with=queryString#andAnchor Full Domain Name With Path to File https://www.domain.com/some-path/to/a/file https://www.domain.com/some-path/to/a/file?with=queryString https://www.domain.com/some-path/to/a/file#withAnchor https://www.domain.com/some-path/to/a/file?with=queryString#andAnchor Full Domain Name With Path to File With Extension https://www.domain.com/some-path/to/a/file-with.ext https://www.domain.com/some-path/to/a/file-with.ext?with=queryString https://www.domain.com/some-path/to/a/file-with.ext https://www.domain.com/some-path/to/a/file-with.ext?with=queryString#andAnchor Dynamic Protocol Full Domain //www.domain.com/ //www.domain.com/?with=queryString //www.domain.com/#withAnchor //www.domain.com/?with=queryString#andAnchor Dynamic Protocol (Full Domain) to File //www.domain.com/with-file //www.domain.com/with-file?with=queryString //www.domain.com/with-file#withAnchor //www.domain.com/with-file?with=queryString#andAnchor Dynamic Protocol (Full Domain) to File With Extension //www.domain.com/with-file-with.ext //www.domain.com/with-file-with.ext?with=queryString //www.domain.com/with-file-with.ext#withAnchor //www.domain.com/with-file-with.ext?with=queryString#andAnchor Dynamic Protocol (Full Domain), Path Leading to File //www.domain.com/with/path/to/file //www.domain.com/with/path/to/file?with=queryString //www.domain.com/with/path/to/file#withAnchor //www.domain.com/with/path/to/file?with=queryString#andAnchor Dynamic Protocol (Full Domain), Path Leading to File With Extension //www.domain.com/with/path/to/file-with.ext //www.domain.com/with/path/to/file-with.ext?with=queryString //www.domain.com/with/path/to/file-with.ext#withAnchor //www.domain.com/with/path/to/file-with.ext?with=queryString#andAnchor Root Relative File /some-root-file /some-root-file?with=queryString /some-root-file#withAnchor /some-root-file?with=queryString#andAnchor Root Relative File With Extension /root-file-with.ext /root-file-with.ext?with=queryString /root-file-with.ext#withAnchor /root-file-with.ext?with=queryString#andAnchor Root Path to File /some-root-path/to/a/file /some-root-path/to/a/file?with=queryString /some-root-path/to/a/file#withAnchor /some-root-path/to/a/file?with=queryString#andAnchor Root Path to File With Extension /some-root-path/to/a/file-with.ext /some-root-path/to/a/file-with.ext?with=queryString /some-root-path/to/a/file-with.ext#withAnchor /some-root-path/to/a/file-with.ext?with=queryString#andAnchor Relative (Same As Requestor) File ./relative-to-requester-file ./relative-to-requester-file?with=queryString ./relative-to-requester-file#withAnchor ./relative-to-requester-file?with=queryString#andAnchor Relative (Same As Requestor) File With Extension ./relative-to-requester-file-with.ext ./relative-to-requester-file-with.ext?with=queryString ./relative-to-requester-file-with.ext#withAnchor ./relative-to-requester-file-with.ext?with=queryString#andAnchor Relative (Same As Requestor) Path To File ./relative-to-requester-path/to/a/file ./relative-to-requester-path/to/a/file?with=queryString ./relative-to-requester-path/to/a/file#withAnchor ./relative-to-requester-path/to/a/file?with=queryString#andAnchor Relative (Same As Requestor) Path To File With Extension ./relative-to-requester-path/to/a/file-with.ext ./relative-to-requester-path/to/a/file-with.ext?with=queryString ./relative-to-requester-path/to/a/file-with.ext#withAnchor ./relative-to-requester-path/to/a/file-with.ext?with=queryString#andAnchor Relative (Above Requestor) File ../relative-file-up ../relative-file-up?with=queryString ../relative-file-up#withAnchor ../relative-file-up?with=queryString#andAnchor Relative (Above Requestor) File With Extension ../relative-file-up-with.ext ../relative-file-up-with.ext?with=queryString ../relative-file-up-with.ext#withAnchor ../relative-file-up-with.ext?with=queryString#andAnchor Relative (two Above Requestor) File ../../relative-file-two-up ../../relative-file-two-up?with=queryString ../../relative-file-two-up#withAnchor ../../relative-file-two-up?with=queryString#andAnchor Relative (two Above Requestor) File With Extension ../../relative-file-two-up.ext ../../relative-file-two-up.ext?with=queryString ../../relative-file-two-up.ext#withAnchor ../../relative-file-two-up.ext?with=queryString#andAnchor Relative (n Above Requestor) File ../../../../../../../../relative-file-n-up ../../../../../../../../relative-file-n-up?with=queryString ../../../../../../../../relative-file-n-up#withAnchor ../../../../../../../../relative-file-n-up?with=queryString#andAnchor Relative Path (Above Requestor) to File ../relative-path/up/to/a/file ../relative-path/up/to/a/file?with=queryString ../relative-path/up/to/a/file#withAnchor ../relative-path/up/to/a/file?with=queryString#andAnchor Relative Path (Above Requestor) to File With Extension ../relative-path/up/to/a/file-with.ext ../relative-path/up/to/a/file-with.ext?with=queryString ../relative-path/up/to/a/file-with.ext#withAnchor ../relative-path/up/to/a/file-with.ext?with=queryString#andAnchor Relative Path (Two Above Requestor) to File ../../relative-path/two/up/to/a/file ../../relative-path/two/up/to/a/file?with=queryString ../../relative-path/two/up/to/a/file#withAnchor ../../relative-path/two/up/to/a/file?with=queryString#andAnchor Relative Path (Two Above Requestor) to File With Extension ../../relative-path/two/up/to/a/file-with.ext ../../relative-path/two/up/to/a/file-with.ext?with=queryString ../../relative-path/two/up/to/a/file-with.ext#withAnchor ../../relative-path/two/up/to/a/file-with.ext?with=queryString#andAnchor Relative Path (n Above Requestor) to File ../../../../../relative-path/n/up/to/a/file ../../../../../relative-path/n/up/to/a/file?with=queryString ../../../../../relative-path/n/up/to/a/file#withAnchor ../../../../../relative-path/n/up/to/a/file?with=queryString#andAnchor Relative Path (n Above Requestor) to File With Extension ../../../../../relative-path/n/up/to/a/file-with.ext ../../../../../relative-path/n/up/to/a/file-with.ext?with=queryString ../../../../../relative-path/n/up/to/a/file-with.ext#withAnchor ../../../../../relative-path/n/up/to/a/file-with.ext?with=queryString#andAnchor "; RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline; foreach (Match m in Regex.Matches(input, pattern, options)) { Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index); } } }

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 C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx