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
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
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • 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

$re = '/(?<!\w)((?<visa>4\d{3}([\ -]?\d{4})(?3)(?3)([\s-]?\d{3})?)|(?<mastercard>(?:5[1-5]\d{2}|222[1-9]|22[3-9]\d|2[3-6]\d{2}|27[01]\d|2720)(?3)(?3)(?3))|(?<amex>3[47]\d{2}([\s-]?\d{5})\d(?7))|(?<discover>6(?:011|[45]\d{2}|22[1-9]|2[4-6]\d|28[2-8])(?3)(?3)(?3)(?:\d{1,3})?)|(?<dc>3(?:[68-9]\d{2}|095|0[0-5]\d)(?:([\s-]?\d{6})|(?3)(?3))(?3)(?4)?)|(?<jcb>35[2-8]\d(?3)(?3)(?3)(?4)?)|(?<dankort>5019(?3)(?3)(?3))|(?<other>(?:5[06-9]\d{2}|5[6-9]\d{2}|6[0-9]\d{2})(?:(?3)(?3)(?3)(?4)?|(?10)(?7))))(?!\w)/m'; $str = '# Sanitize Tests Regex created primarily created from [Wikipedia]. Test number sources: * https://www.freeformatter.com/credit-card-number-generator-validator.html * https://www.paypalobjects.com/en_GB/vhelp/paypalmanager_help/credit_card_numbers.htm * https://developers.bluesnap.com/docs/test-credit-cards * http://ccardgen.com/generate-card/interpayment VISA: 4532177128633695 4916680636480117 4539967634659629604 MasterCard: 5223313008672716 2221004141297342 5558230861887444 American Express (AMEX): 342615995508555 341566355817758 340016258420319 Discover: 6011028088400481 6011146024349255 60115564485789458 6011520414457823826 JCB: 3589047185943333 3535222682062052 3545291203766687023 Diners Club - North America: 5502801000658032 5414150073102768 5439896982508161 Diners Club - Carte Blanche: 30326507490665 30016760488007 30567301198810 Diners Club - International: 36116970193449 36028617349161 36791828122269 Maestro: 604952990772800 604480375435066 6761293209786109 6799990100000000019 Visa Electron: 4508523227492030 4508296845966301 4175000962812757 InstaPayment: 6372237262247088 6389000629384874 6392933658984245 InterPayment: 6365364106904019758 # PAYPAL American Express 378282246310005 American Express 371449635398431 American Express Corporate 378734493671000 Australian BankCard 5610591081018250 * Maestro? (BankCard defunct) Diners Club 30569309025904 * Inactive according to DC Diners Club 38520000023237 * Inactive according to DC Discover 6011111111111117 Discover 6011000990139424 JCB 3530111333300000 JCB 3566002020360505 MasterCard 5555555555554444 MasterCard 5105105105105100 Visa 4111111111111111 Visa 4012888888881881 Visa 4222222222222 * Inactive (14 digits) Dankort (PBS) 76009244561 * Inactive Dankort (PBS) 5019717010103742 Switch/Solo (Paymentech) 6331101999990016 * Inactive # Bluesnap Amex 374245455400126 05/2023 Success Amex 378282246310005 05/2023 Failure Discover 60115564485789458 12/2023 US USD Success Discover 6011000991300009 12/2023 US USD Success JCB 3566000020000410 02/2023 123 JP JPY Success JCB 3530111333300000 03/2023 JP JPY Failure Mastercard 5425233430109903 04/2023 Success Mastercard 5425233430109903 12/2004 Invalid exp. date Mastercard 2222420000001113 08/2020 Success Mastercard 2223000048410010 09/2020 Success Visa 4263982640269299 02/2023 837 Success Visa 4263982640269299 04/2023 738 Success Visa 4917484589897107 01/2023 ARS EUR USD Failure Visa1 4001919257537193 09/2019 BR BRL Success Visa1 4007702835532454 10/2020 BR BRL Success ELO1 6362970000457013 8/2019 BR BRL Success Hipercard1 6062826786276634 09/2020 BR BRL Success Argencard1 5011054488597827 12/2019 AR ARS Success Cabal1 6271701225979642 03/2020 AR ARS Success Cencosud1 6034932528973614 06/2020 AR ARS Success Naranja1 5895626746595650 11/2020 AR ARS Success Nativa1 5200533989557118 11/2019 AR ARS Success Tarjeta Shopping1 6034883265619896 09/2020 AR ARS Success # Simplify 2222 4053 4324 8877 MasterCard APPROVED APPROVED 2222 9909 0525 7051 MasterCard APPROVED APPROVED 2223 0076 4872 6984 MasterCard APPROVED APPROVED 2223 5771 2001 7656 MasterCard APPROVED APPROVED 5105 1051 0510 5100 MasterCard APPROVED APPROVED 5111 0100 3017 5156 MasterCard APPROVED APPROVED 5185 5408 1000 0019 MasterCard APPROVED APPROVED 5200 8282 8282 8210 MasterCard APPROVED APPROVED 5204 2300 8000 0017 MasterCard APPROVED APPROVED 5204 7400 0990 0014 MasterCard APPROVED APPROVED 5420 9238 7872 4339 MasterCard APPROVED APPROVED 5455 3307 6000 0018 MasterCard APPROVED APPROVED 5506 9004 9000 0436 MasterCard APPROVED APPROVED 5506 9004 9000 0444 MasterCard APPROVED APPROVED 5506 9005 1000 0234 MasterCard APPROVED APPROVED 5506 9208 0924 3667 MasterCard APPROVED APPROVED 5506 9224 0063 4930 MasterCard APPROVED APPROVED 5506 9274 2731 7625 MasterCard APPROVED APPROVED 5553 0422 4198 4105 MasterCard APPROVED APPROVED 5555 5537 5304 8194 MasterCard APPROVED APPROVED 5555 5555 5555 4444 MasterCard APPROVED APPROVED 4012 8888 8888 1881 Visa APPROVED APPROVED 4111 1111 1111 1111 Visa APPROVED APPROVED 6011 0009 9013 9424 Discover APPROVED APPROVED 6011 1111 1111 1117 Discover APPROVED APPROVED 3714 496353 98431 American Express APPROVED APPROVED 3782 822463 10005 American Express APPROVED APPROVED 3056 930902 5904 Diners Intl APPROVED APPROVED * Inactive according to DC 3852 000002 3237 Diners Intl APPROVED APPROVED * Inactive according to DC 3530 1113 3330 0000 JCB APPROVED APPROVED 3566 0020 2036 0505 JCB APPROVED APPROVED [Wikipedia]: https://en.wikipedia.org/wiki/Payment_card_number#cite_note-Barclaycard-15'; 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