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 = "^(0|84)(2(0[3-9]|1[0-689]|2[0-25-9]|3[2-9]|4[0-9]|5[124-9]|6[0369]|7[0-7]|8[0-9]|9[012346789])|3[2-9]|5[25689]|7[06-9]|8[0-9]|9[012346789])([0-9]{7})$"; final String string = "0917123123\n" + "0901123123\n" + "0931123123\n" + "0891123123\n" + "0701123123\n" + "0761123123\n" + "0771123123\n" + "0781123123\n" + "0791123123\n" + "01201123123\n" + "01211123123\n" + "01221123123\n" + "01261123123\n" + "01281123123\n" + "0911123123\n" + "0941123123\n" + "0881123123\n" + "0811123123\n" + "0821123123\n" + "0831123123\n" + "0841123123\n" + "0851123123\n" + "01231123123\n" + "01241123123\n" + "01251123123\n" + "01271123123\n" + "01291123123\n" + "0961123123\n" + "0971123123\n" + "0981123123\n" + "0861123123\n" + "0321123123\n" + "0331123123\n" + "0341123123\n" + "0351123123\n" + "0361123123\n" + "0371123123\n" + "0381123123\n" + "0391123123\n" + "01621123123\n" + "01631123123\n" + "01641123123\n" + "01651123123\n" + "01661123123\n" + "01671123123\n" + "01681123123\n" + "01691123123\n" + "0921123123\n" + "0561123123\n" + "0581123123\n" + "0521123123\n" + "01881123123\n" + "01861123123\n" + "0991123123\n" + "0591123123\n" + "01991123123\n" + "0871123123\n" + "0551123123"; 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