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

/
/
gm

Test String

Code Generator

Generated Code

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

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 AutoIt, please visit: https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm