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

/
/
x

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 = "^ (?&address) $\n" + "(?(DEFINE)\n" + " (?<ALPHANUMERICE> \n" + " [A-Za-z0-9!\\#$%&'*+\\-\\/=?^_`\\{|\\}~]\n" + " )\n" + " (?<CFWS> \n" + " (?: \n" + " (?: (?&FWS)? (?&comment) )++ \n" + " (?&FWS)?\n" + " ) \n" + " | (?&FWS) \n" + " )\n" + " (?<FWS> (?&WSPCRLF)++ )\n" + " (?<UTF8_extra_char> \n" + " (?=[\\x{C2}-\\x{DF}\\x{E0}-\\x{EF}\\x{F0}-\\x{F4}]) \n" + " (?: (?&UTF8_2) | (?&UTF8_3) | (?&UTF8_4) ) \n" + " )\n" + " (?<UTF8_tail> [\\x{80}-\\x{BF}] )\n" + " (?<UTF8_2> [\\x{C2}-\\x{DF}] (?&UTF8_tail) )\n" + " (?<UTF8_3> \\x{E0} [\\x{A0}-\\x{BF}] (?&UTF8_tail)\n" + " | [\\x{E1}-\\x{EC}] (?&UTF8_tail){2} \n" + " | \\x{ED} [\\x{80}-\\x{9F}] (?&UTF8_tail) \n" + " | [\\x{EE}\\x{EF}] (?&UTF8_tail){2}\n" + " )\n" + " (?<UTF8_4> \\x{F0} [\\x{90}-\\x{BF}] (?&UTF8_tail){2}\n" + " | [\\x{F1}-\\x{F3}] (?&UTF8_tail){3}\n" + " | \\x{F4} [\\x{80}-\\x{8F}] (?&UTF8_tail){2}\n" + " )\n" + " (?<VCHARWSP> [\\x{21}-\\x{7E}\\ \\t] )\n" + " (?<WSPCRLF> [\\ \\t\\x{0D}\\x{0A}] )\n" + " (?<address> (?&mailbox_list) | (?&group) )\n" + " (?<addr_spec> (?&local_part) @ (?&domain) )\n" + " (?<utf8_addr_spec> \n" + " (?&utf8_local_part) @ (?&utf8_domain) \n" + " )\n" + " (?<alt_address> (?&FWS) < (?&addr_spec) > )\n" + " (?<angle_addr> (?&CFWS)? < \n" + " (?: (?&addr_spec) \n" + " | (?&utf8_addr_spec) (?&alt_address)? \n" + " | (?&obs_route)? (?&addr_spec) \n" + " ) \n" + " > (?&CFWS)? \n" + " )\n" + " (?<atext> (?&ALPHANUMERICE) )\n" + " (?<utf8_atext> \n" + " (?&ALPHANUMERICE) \n" + " | (?&UTF8_extra_char) \n" + " )\n" + " (?<atom> (?&CFWS)? (?&atext)++ (?&CFWS)? )\n" + " (?<utf8_atom> \n" + " (?&CFWS)? (?&utf8_atext)++ (?&CFWS)? \n" + " )\n" + " (?<utf8_ccontent> \n" + " (?&ctext) \n" + " | (?&utf8_quoted_pair) \n" + " | (?&comment) \n" + " )\n" + " (?<comment> \n" + " \\( \n" + " (?: \n" + " (?&FWS)? (?&utf8_ccontent) \n" + " )*+ \n" + " (?&FWS)? \n" + " \\) \n" + " )\n" + " (?<ctext> \n" + " [!-'*-\\[\\]-~] \n" + " | (?&obs_ctext) \n" + " | (?&UTF8_extra_char) \n" + " )\n" + " (?<display_name> (?&phrase) )\n" + " (?<domain> \n" + " (?&dot_atom) \n" + " | (?&domain_literal) \n" + " | (?&obs_domain) \n" + " )\n" + " (?<utf8_domain> \n" + " (?&utf8_dot_atom) \n" + " | (?&domain_literal) \n" + " | (?&obs_domain) \n" + " )\n" + " (?<domain_literal> \n" + " (?&CFWS)? \n" + " \\[ \n" + " (?: (?&FWS)? (?&dtext) )*+ \n" + " (?&FWS)? \n" + " \\] \n" + " (?&CFWS)? \n" + " )\n" + " (?<dot_atom> \n" + " (?&CFWS)? (?&dot_atom_text) (?&CFWS)? \n" + " )\n" + " (?<utf8_dot_atom> \n" + " (?&CFWS)? (?&utf8_dot_atom_text) (?&CFWS)? \n" + " )\n" + " (?<dot_atom_text> \n" + " (?&atext)++ \n" + " (?: \\. (?&atext)++ )*+ \n" + " )\n" + " (?<utf8_dot_atom_text> \n" + " (?&utf8_atext)++ \n" + " (?: \\. (?&utf8_atext)++ )*+ \n" + " )\n" + " (?<dtext> [!-Z^-~] | (?&obs_dtext) )\n" + " (?<group> \n" + " (?&display_name) : (?&group_list)? ; (?&CFWS)? \n" + " )\n" + " (?<group_list> \n" + " (?&mailbox_list) \n" + " | (?&CFWS) \n" + " | (?&obs_group_list) \n" + " )\n" + " (?<local_part> \n" + " (?&dot_atom) \n" + " | (?&quoted_string) \n" + " | (?&obs_local_part) \n" + " )\n" + " (?<utf8_local_part> \n" + " (?&utf8_dot_atom) \n" + " | (?&utf8_quoted_string) \n" + " | (?&obs_local_part) \n" + " )\n" + " (?<mailbox> \n" + " (?&name_addr) \n" + " | (?&addr_spec) \n" + " | (?&utf8_addr_spec) \n" + " )\n" + " (?<mailbox_list> \n" + " (?: \n" + " (?&mailbox) (?: , (?&mailbox) )*+ \n" + " ) \n" + " | (?&obs_mbox_list) \n" + " ) \n" + " (?<name_addr> (?&display_name)? (?&angle_addr) )\n" + " (?<obs_NO_WS_CTL> \n" + " [\\x{01}-\\x{08}\\x{0B}\\x{0C}\\x{0E}-\\x{1F}\\x{7F}] \n" + " )\n" + " (?<obs_ctext> (?&obs_NO_WS_CTL) )\n" + " (?<obs_domain> (?&atom) (?: \\. (?&atom) )*+ )\n" + " (?<obs_domain_list> \n" + " (?: (?&CFWS) | , )*+ \n" + " @ (?&domain) \n" + " (?: , (?&CFWS)? (?: @ (?&domain) )? )*+ \n" + " )\n" + " (?<obs_dtext> \n" + " (?&obs_NO_WS_CTL) | (?&quoted_pair) \n" + " )\n" + " (?<obs_group_list> (?: (?&CFWS)? , )++ (?&CFWS)? )\n" + " (?<obs_local_part> (?&word) (?: \\. (?&word) )*+ )\n" + " (?<obs_mbox_list> \n" + " (?: (?&CFWS)? , )*+ \n" + " (?&mailbox) \n" + " (?: , \n" + " (?: (?&mailbox) | (?&CFWS))? \n" + " )*+ \n" + " )\n" + " (?<obs_phrase> \n" + " (?&word) \n" + " (?: (?&word) | \\. | (?&CFWS) )*+ \n" + " )\n" + " (?<obs_qp> \n" + " \\\\ \n" + " (?: \n" + " \\x{00} \n" + " | (?&obs_NO_WS_CTL) \n" + " | [\\x{0D}\\x{0A}] \n" + " ) \n" + " )\n" + " (?<obs_qtext> (?&obs_NO_WS_CTL) )\n" + " (?<obs_route> (?&obs_domain_list) : )\n" + " (?<phrase> (?&word)++ | (?&obs_phrase) )\n" + " (?<qcontent> (?&utf8_qcontent) )\n" + " (?<utf8_qcontent> \n" + " (?&utf8_qtext) \n" + " | (?&utf8_quoted_pair) \n" + " )\n" + " (?<qtext> [!\\#-\\[\\]-~] | (?&obs_qtext) )\n" + " (?<utf8_qtext> (?&qtext) | (?&UTF8_extra_char) )\n" + " (?<quoted_pair> \n" + " \\\\ (?&VCHARWSP) | (?&obs_qp) \n" + " )\n" + " (?<utf8_quoted_pair> \n" + " \\\\ (?&utf8_text) | (?&obs_qp) \n" + " )\n" + " (?<utf8_text> \n" + " [\\x{01}-\\x{09}\\x{0D}\\x{0A}\\x{0E}-\\x{7F}] \n" + " | (?&UTF8_extra_char) \n" + " )\n" + " (?<quoted_string> \n" + " (?&CFWS)? \" \n" + " (?: (?&FWS)? (?&qcontent) )*+ \n" + " (?&FWS)? \" (?&CFWS)? \n" + " )\n" + " (?<utf8_quoted_string> \n" + " (?&CFWS)? \" \n" + " (?: (?&FWS)? (?&utf8_qcontent) )*+ \n" + " (?&FWS)? \" (?&CFWS)? \n" + " )\n" + " (?<word> (?&utf8_atom) | (?&utf8_quoted_string) )\n" + ")"; final String string = "John <a@b.c>, w@com, \"wibb.[dhdhd]\"@(comment)fred.com"; final Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS); final Matcher matcher = pattern.matcher(string); if (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