Regular Expressions Examples: A Comprehensive List

Regular expressions are a powerful tool used for pattern matching in text. They are used in a wide range of applications like text editors, programming languages, and data validation. This article will provide a comprehensive list of regular expression examples.

Quick Guide

\the escape character – used to find an instance of a metacharacter like a period, brackets, etc.
.match any character except newline
xmatch any instance of x
^xmatch any character except x
[x]match any instance of x in the bracketed range – [abxyz] will match any instance of a, b, x, y, or z
|an OR operator – [x|y] will match an instance of x or y
()used to group sequences of characters or matches
{}used to define numeric quantifiers
{x}match must occur exactly x times
{x,}match must occur at least x times
{x,y}match must occur at least x times, but no more than y times
?preceding match is optional or one only, same as {0,1}
*find 0 or more of preceding match, same as {0,}
+find 1 or more of preceding match, same as {1,}
^match the beginning of the line
$match the end of a line
[:alpha:]Represents an alphabetic character. Use [:alpha:]+ to find one of them.
[:digit:]Represents a decimal digit. Use [:digit:]+ to find one of them.
[:alnum:]Represents an alphanumeric character ([:alpha:] and [:digit:]).
[:space:]Represents a space character (but not other whitespace characters).
[:print:]Represents a printable character.
[:cntrl:]Represents a nonprinting character.
[:lower:]Represents a lowercase character if Match case is selected in Options.
[:upper:]Represents an uppercase character if Match case is selected in Options.
\dmatches a digit, same as [0-9]
\Dmatches a non-digit, same as [^0-9]
\smatches a whitespace character (space, tab, newline, etc.)
\Smatches a non-whitespace character
\wmatches a word character
\Wmatches a non-word character
\bmatches a word-boundary (NOTE: within a class, matches a backspace)
\Bmatches a non-wordboundary

Table of Contents

Regular Expression with Example

  • Email Address

    RegEx: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,3}$

    ⬆ Back to Top

  • Number

    RegEx: ^[0-9]*$

    ⬆ Back to Top

  • Number for 10 digit

    RegEx: ^[0-9]{10}$

    ⬆ Back to Top

  • Allow symbols in number

    Allow symbols: -,+,(,)

    RegEx: ^(?=.*[0-9])[- +()0-9]{10,14}$

    ⬆ Back to Top

  • URL

    RegEx: ^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$

    ⬆ Back to Top

  • Percentage

    RegEx: ^((\d{0,2}(\.\d{1,4})?)|100)$

    ⬆ Back to Top

  • Username (Alphanumeric with underscore)

    RegEx: ^[a-zA-Z0-9_]+$

    ⬆ Back to Top

  • Password (At least 8 characters, one uppercase, one lowercase, one digit, and one special character)

    RegEx: ^(?=.[a-z])(?=.[A-Z])(?=.\d)(?=.[@$!%?&])[A-Za-z\d@$!%?&]{8,}$

    ⬆ Back to Top

  • Phone Number (US)

    RegEx: ^(\d{3}-\d{3}-\d{4}|\d{10})$

    ⬆ Back to Top

  • ZIP Code (US)

    RegEx: ^\d{5}$|^\d{5}-\d{4}$

    ⬆ Back to Top

  • Hexadecimal Color Code (3 or 6 digits)

    RegEx: ^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$

    ⬆ Back to Top

  • IP Address (IPv4)

    RegEx: ^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

    ⬆ Back to Top

  • Date (YYYY-MM-DD)

    RegEx: ^(\d{4})-(\d{2})-(\d{2})$

    ⬆ Back to Top

  • 24 Hour Time (hh:mm)

    RegEx: ^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$

    ⬆ Back to Top

  • Card CVV

    RegEx: ^[0-9]{3,4}$

    ⬆ Back to Top

  • Credit Card Expiration Date (MM/YYYY)

    RegEx: ^(0[1-9]|1[0-2])\/(20)\d{2}$

    ⬆ Back to Top

  • Credit Card Expiry Date (MM/YY)

    RegEx: (0[1-9]|10|11|12)\/[0-9]{2}|\.

    ⬆ Back to Top

  • Credit Card Number (Visa, Mastercard, American Express, Discover)

    RegEx: ^(?:4[0-9]{12}(?:[0-9]{3})?|[25][1-7][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$

    ⬆ Back to Top

  • Amex Card Number

    RegEx: ^3[47][0-9]{13}$

    ⬆ Back to Top

  • BCGlobal Card Number

    RegEx: ^(6541|6556)[0-9]{12}$

    ⬆ Back to Top

  • Carte Blanche Card Number

    RegEx: ^389[0-9]{11}$

    ⬆ Back to Top

  • Diners Club Card Number

    RegEx: ^3(?:0[0-5]|[68][0-9])[0-9]{11}$

    ⬆ Back to Top

  • Discover Card Number

    RegEx: ^65[4-9][0-9]{13}|64[4-9][0-9]{13}|6011[0-9]{12}|(622(?:12[6-9]|1[3-9][0-9]|[2-8][0-9][0-9]|9[01][0-9]|92[0-5])[0-9]{10})$

    ⬆ Back to Top

  • Insta Payment Card Number

    RegEx: ^63[7-9][0-9]{13}$

    ⬆ Back to Top

  • JCB Card Number

    RegEx: ^(?:2131|1800|35\d{3})\d{11}$

    ⬆ Back to Top

  • Korean Local Card Number

    RegEx: ^9[0-9]{15}$

    ⬆ Back to Top

  • Laser Card Number

    RegEx: ^(6304|6706|6709|6771)[0-9]{12,15}$

    ⬆ Back to Top

  • Maestro Card Number

    RegEx: ^(5018|5020|5038|6304|6759|6761|6763)[0-9]{8,15}$

    ⬆ Back to Top

  • Master Card Number

    RegEx: ^(5[1-5][0-9]{14}|2(22[1-9][0-9]{12}|2[3-9][0-9]{13}|[3-6][0-9]{14}|7[0-1][0-9]{13}|720[0-9]{12}))$

    ⬆ Back to Top

  • Solo Card Number

    RegEx: ^(6334|6767)[0-9]{12}|(6334|6767)[0-9]{14}|(6334|6767)[0-9]{15}$

    ⬆ Back to Top

  • Switch Card Number

    RegEx: ^(4903|4905|4911|4936|6333|6759)[0-9]{12}|(4903|4905|4911|4936|6333|6759)[0-9]{14}|(4903|4905|4911|4936|6333|6759)[0-9]{15}|564182[0-9]{10}|564182[0-9]{12}|564182[0-9]{13}|633110[0-9]{10}|633110[0-9]{12}|633110[0-9]{13}$

    ⬆ Back to Top

  • Union Pay Card Number

    RegEx: ^(62[0-9]{14,17})$

    ⬆ Back to Top

  • Visa Card Number

    RegEx: ^4[0-9]{12}(?:[0-9]{3})?$

    ⬆ Back to Top

  • Visa Master Card Number

    RegEx: ^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14})$

    ⬆ Back to Top

Keep on programming! 😊

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *