Regular Expressions 101

Save & Share

  • Regex Version: ver. 14
  • Update Regex
    ctrl+⇧+s
  • Save new Regex
    ctrl+s
  • Add to Community Library

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

/
/
gsx

Test String

Code Generator

Generated Code

$re = '/(?<![-!#$%&\'*+\/=?^_`{|}~@."\]\\\\a-zA-Zа-яА-ЯёЁ\d]) #граница (?: [-!#$%&\'*\/=?^_`{|}~a-zA-Z\d\+]+ | [-!#$%&\'*\/=?^_`{|}~а-яА-ЯёЁ\d\+]+ | "(?:(?:[^"\\\\]|\\\\.)+)" ) (?: [.+] (?: #просмотр вперёд с обратной ссылкой запрещает "откат" (backtrace) в случае неудачи и значительно ускоряет работу цикла внутри цикла (?=([-!#$%&\'*\/=?^_`{|}~a-zA-Z\d]+))\1 | (?=([-!#$%&\'*\/=?^_`{|}~а-яА-ЯёЁ\d]+))\2 | "(?=((?:[^"\\\\]|\\\\.)+))\3" ) )* @ (?: (?: (?: #домены 2-го и последующих уровней (?!-) (?: (?:[a-zA-Z\d]|-(?!-)){1,63} | (?:[а-яА-ЯёЁ\d]|-(?!-)){1,63} ) (?<!-) \. )+ (?: #домен 1-го уровня [a-zA-Z]{2,63} | [а-яА-ЯёЁ]{2,63} ) ) (?![a-zA-Zа-яА-ЯёЁ\d@]) #граница | (?: #IPv4 (?<!\d) (?!0+\.) (?:1?\d\d?|2(?:[0-4]\d|5[0-5]))(?:\.(?:1?\d\d?|2(?:[0-4]\d|5[0-5]))){3} (?!\d) ) | \[ #IPv4 в квадратных скобках (?: (?<!\d) (?!0+\.) (?:1?\d\d?|2(?:[0-4]\d|5[0-5]))(?:\.(?:1?\d\d?|2(?:[0-4]\d|5[0-5]))){3} (?!\d) ) \] )/sx'; $str = 'Valid email addresses --------------------- ПишитеМне@ИвановИван.почта.рф Иванов.Иван@Санкт-Петербург.онлайн Ivanov.Иван@москва.ru Ivanov.Иван+facebook@москва.ru c++@test.com prettyandsimple@example.com very.common@example.com disposable.style.email.with+symbol@example.com other.email-with-dash@example.com x@example.com (one-letter local part) "much.more unusual"@example.com "very.unusual.@.unusual.com"@example.com "very.(),:;<>[]\\".VERY.\\"very@\\\\ \\"very\\".unusual"@strange.example.com example-indeed@strange-example.com #!$%&\'*+-/=?^_`{}|~@example.org "()<>[]:,;@\\\\\\"!#$%&\'*+-/=?^_`{}| ~.a"@example.org " "@example.org (space between the quotes) example@s.solutions (see the List of Internet top-level domains) ©other.email-with-dash@example.com ?prettyandsimple@example.com Эти email валидные только среде разработки. В продуктивной среде такие email пользователи применять не могут и поэтому считаются невалидными: * admin@mailserver1 (local domain name with no TLD) * example@localhost (sent from localhost) * user@com * user@localserver * user@[IPv6:2001:db8::1] Valid Email address Reason -------------------------- email@domain.com Valid email firstname.lastname@domain.com Email contains dot in the address field email@subdomain.domain.com Email contains dot with subdomain firstname+lastname@domain.com Plus sign is considered valid character email@123.123.123.123 Domain is valid IP address email@[123.123.123.123] Square bracket around IP address is considered valid "email"@domain.com Quotes around email is considered valid 1234567890@domain.com Digits in address are valid email@domain-one.com Dash in domain name is valid _______@domain.com Underscore in the address field is valid email@domain.name .name is valid Top Level Domain name email@domain.co.jp Dot in Top Level Domain name also considered valid (use co.jp as example here) firstname-lastname@domain.com Dash in address field is valid Invalid Email address Reason ------------------------------ plainaddress Missing @ sign and domain #@%^%#$@#$@#.com Garbage @domain.com Missing username Joe Smith <email@domain.com> Encoded html within email is invalid email.domain.com Missing @ email@domain@domain.com Two @ sign .email@domain.com Leading dot in address is not allowed email.@domain.com Trailing dot in address is not allowed email..email@domain.com Multiple dots あいうえお@domain.com Unicode char as address email@domain Missing top level domain (.com/.net/.org/etc) email@-domain.com Leading dash in front of domain is invalid email@domain.web .web is not a valid top level domain email@111.222.333.44444 Invalid IP format email@domain..com Multiple dot in the domain portion is invalid Abc.example.com (no @ character) A@b@c@example.com (only one @ is allowed outside quotation marks) a"b(c)d,e:f;g<h>i[j\\k]l@example.com (none of the special characters in this local part are allowed outside quotation marks) just"not"right@example.com (quoted strings must be dot separated or the only element making up the local part) this is"not\\allowed@example.com (spaces, quotes, and backslashes may only exist when within quoted strings and preceded by a backslash) this\\ still\\"not\\\\allowed@example.com (even if escaped (preceded by a backslash), spaces, quotes, and backslashes must still be contained by quotes) john..doe@example.com (double dot before @) with caveat: Gmail lets this through, Email address#Local-part the dots altogether john.doe@example..com (double dot after @) '; preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0); // Print the entire match result var_dump($matches);

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 PHP, please visit: http://php.net/manual/en/ref.pcre.php