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"
"
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 = "^(?![^a-zA-Z0-9])(?:(?![.]{2,})[a-zA-Z0-9_\\-.])+(?<![.])@(?:(?![^a-zA-Z0-9])(?:[a-zA-Z0-9_\\-])+(?<![^a-zA-Z0-9])\\.)+[a-zA-Z]{2,}$"; final String string = "email@example.com\n" + "email@111.222.333.fr\n" + "firstname.lastname@example.com\n" + "email@subdomain.example.com\n" + "123@gmail.com\n" + "123@51gmail62548.com\n" + "1234567890@example.com\n" + "a.defosseux@tbs-education.fr\n" + "email@example-one.com\n" + "email@example.name\n" + "email@example.museum\n" + "email@example.co.jp\n" + "firstname-lastname@example.com\n" + "test@test.com\n" + "ezf@aezf.fr.en\n" + "azer123@123.fr\n" + "a@a.bb\n" + "a@a.ccc\n" + "a@bb.bb\n" + "a.a@bb.bb\n" + "a.a.a@bb.bb\n" + "a.a.a@a.bb.bb\n" + "a.a.a.a@a.a.bb\n" + "a.a.a.a@a.bb.bb\n" + "a.a.a.a@a.bb.ccc\n" + "a.bb.a.a@a.bb.ccc\n" + "a--a.a@a.bb.ccc\n" + "a.defosseux@tbs-education.x.fr\n" + "a.defosseux@tbs-education.x.y.fr\n" + "a.defosseux@tbs-education.x-e.y.fr\n" + "email@example-one.com\n" + "ema.il@example.com\n" + "e.ma.il@example.com\n" + "ema-il@example.com\n" + "e-ma-il@example.com\n" + "ema--il@example.com\n" + "em_ail@example.com\n" + "em__ail@example.com\n" + "6email@example.com\n" + "email6@example.com\n" + "6email6@example.com\n" + "test-test@x.fr\n" + "tes-t-test@x.fr\n" + "yasmina.achak@e.rascol.net\n" + "yasmina.achak@e.rascol.net.aezr.azerz.azer.azreezr.azeraez.azer\n" + "yasmina.achak@e.rascol.barce\n" + "yasmina.achak@e.rascol.bar\n" + "yasmina.achak@e.ba\n" + "sarah_benabdell@yahoo.fr\n" + "sarah__benabdellah@yahoo.fr\n" + "sarah---benabdellah@yahoo.fr\n" + "sarah___---benabdellah@yahoo.fr\n" + "marieagnes.paul@off---white.com\n" + "a.a@bb--bb.ccc\n" + "a.a@bb--bb.a.ccc\n" + "a_a@a.bb\n" + "a_a@a.bb\n" + "email-@example.com\n" + "email_@example.com\n" + "a_@a.bb\n" + "a._@a.bb\n\n" + "a@ccc\n" + "#@%^%#$@#$@#.com\n" + "@example.com\n" + "Joe Smith <email@example.com>\n" + "email.example.com\n" + "email@example@example.com\n" + "email.@example.com\n" + "email..email@example.com\n" + "あいうえお@example.com\n" + "email@example.com (Joe Smith)\n" + "email@example\n" + "email@-example.com\n" + "email@111.222.333.44444\n" + "email@example..com\n" + "a..reds@free.fr\n" + "Abc..123@example.com\n" + "”(),:;<>[\\]@example.com\n" + "just”not”right@example.com\n" + "this\\ is\"really\"not\\allowed@example.com\n" + "firstname+lastname@example.com\n" + "email@123.123.123.123\n" + "email@[123.123.123.123]\n" + "\"email\"@example.com\n" + "_______@example.com\n" + "much.\"more\\ unusual\"@example.com\n" + "very.unusual.”@”.unusual.com@example.com\n" + "very.”(),:;<>[]”.VERY.”very@\\\\ \"very”.unusual@strange.example.com\n" + "azer aezr@test.com\n" + "zaetazfe.faezf\n" + "af@ezfezf\n" + "ez#!~f@aezf.fr\n" + "azefé@test.com\n" + "aze@zaé.com\n" + "a@a.a\n" + "a.a@a.a\n" + "a.a@a.a.a\n" + "a.a.a.a@a.a.a\n" + "a.a.a.a@a.a.a.a\n" + "a..a.a@a.bb.ccc\n" + "a.é@b.fr\n" + "azer.@fr\n" + ".azer@fr\n" + "a.defosseux@tbs-education-.fr\n" + "a.defosseux@-tbs-education.fr\n" + "a.defosseux@-tbs-education-.fr\n" + "a.defosseux@1234tbs-education.-x-.fr\n" + ".email@example.com\n" + ".email.@example.com\n" + "email.@example.com\n" + "ema..il@example.com\n" + "-email@example.com\n" + "-email-@example.com\n" + "_email@example.com\n" + "_email_@example.com\n" + "_em_ail_@example.com\n" + "_em__ail_@example.com\n" + "yaémina.achak@e.rascol.net\n" + ".-y__as-min-.@eerez.fr\n" + "yasmina.achak@e.b\n" + "yasmina.achak@e....bar\n" + "_a_@a.bb\n" + "_a@a.bb\n" + "_.a@a.bb\n" + "_.a._@a.bb\n" + "a@_a.bb\n" + "a@a_.bb\n" + "a@_a_.bb\n" + "a@a._bb_.ccc\n" + "a@a.__a__a__.ccc\n" + "a@a.__.bb.ccc"; 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