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

/
/
gmi

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 = "^(?:((?:IT|SM)\\d{2}\\s?[A-Z]{1}\\d{3}\\s?(\\d{4}\\s?){4}\\d{3})|(NL\\d{2}\\s?[A-Z]{4}\\s?(\\d{4}\\s?){2}\\d{2})|(LV\\d{2}\\s?[A-Z]{4}\\s?(\\d{4}\\s?){3}\\d{1})|((?:BG|GB|IE)\\d{2}\\s?[A-Z]{4}\\s?(\\d{4}\\s?){3}\\d{2})|(GI\\d{2}\\s?[A-Z]{4}\\s?(\\d{4}\\s?){3}\\d{3})|(RO\\d{2}\\s?[A-Z]{4}\\s?(\\d{4}\\s?){4})|(MT\\d{2}\\s?[A-Z]{4}\\s?(\\d{4}\\s?){5}\\d{3})|(NO\\d{2}\\s?(\\d{4}\\s?){2}\\d{3})|((?:DK|FI|FO)\\d{2}\\s?(\\d{4}\\s?){3}\\d{2})|((?:SI)\\d{2}\\s?(\\d{4}\\s?){3}\\d{3})|((?:AT|EE|LU|LT)\\d{2}\\s?(\\d{4}\\s?){4})|((?:HR|LI|CH)\\d{2}\\s?(\\d{4}\\s?){4}\\d)|((?:DE)\\d{2}\\s?(\\d{4}\\s?){4}\\d{2})|((?:CZ|ES|SK|SE)\\d{2}\\s?(\\d{4}\\s?){5})|(PT\\d{2}\\s?(\\d{4}\\s?){5}\\d)|((?:IS)\\d{24})|((?:BE)\\d{2}\\s?(\\d{4}\\s?){3})|((?:FR|MC|GR)\\d{2}\\s?([0-9A-Z]{4}\\s?){5}\\d{3})|((?:PL|HU|CY)\\d{2}\\s?(\\d{4}\\s?){6}))$"; final String string = "AD14 0008 0001 0012 3456 7890\n" + "BE23 0003 2399 3244\n" + "AT48 3200 0000 1234 5864 \n" + "BE71 0961 2345 6769 \n" + "BG18 RZBB 9155 0123 4567 89\n" + "HR17 2360 0001 1012 3456 5\n" + "CY21 0020 0195 0000 3570 0123 4567\n" + "CZ55 0800 0000 0012 3456 7899\n" + "DK95 2000 0123 4567 89\n" + "EE47 1000 0010 2014 5685 \n" + "FI14 1009 3000 1234 58\n" + "FR76 3000 6000 0112 3456 7890 189\n" + "DE75 5121 0800 1245 1261 99\n" + "GI04 BARC 0000 0123 4567 890\n" + "GR96 0810 0010 0000 0123 4567 890\n" + "HU93 1160 0006 0000 0000 1234 5676\n" + "IS75 0001 1212 3456 3108 9620 99\n" + "IE64 IRCE 9205 0112 3456 78\n" + "IT60 X054 2811 1010 0000 0123 456\n" + "LV97 HABA 0012 3456 7891 0\n" + "LI74 0880 6123 4567 8901 2\n" + "LT60 1010 0123 4567 8901 \n" + "LU12 0010 0012 3456 7891 \n" + "MT31 MALT 0110 0000 0000 0000 0000 123\n" + "MC58 1009 6180 7901 2345 6789 085\n" + "NL02 ABNA 0123 4567 89\n" + "NO83 3000 1234 567\n" + "PL10 1050 0099 7603 1234 5678 9123\n" + "PT50 0027 0000 0001 2345 6783 3\n" + "RO09 BCYP 0000 0012 3456 7890\n" + "SM76 P085 4009 8121 2345 6789 123\n" + "SK89 7500 0000 0000 1234 5671\n" + "SI56 1920 0123 4567 892\n" + "ES79 2100 0813 6101 2345 6789\n" + "SE72 8000 0810 3400 0978 3242\n" + "CH56 0483 5012 3456 7800 9\n" + "GB33 BUKB 2020 1555 5555 55\n" + "VA59 0011 2300 0012 3456 78\n" + "AL35 2021 1109 0000 0000 0123 4567\n" + "AZ96 AZEJ 0000 0000 0012 3456 7890\n" + "BH02 CITI 0000 1077 1816 11\n" + "BA39 3385 8048 0021 1234 \n" + "BR15 0000 0000 0000 1093 2840 814P 2\n" + "CR23 0151 0841 0026 0123 45\n" + "FO92 6460 0123 4567 89\n" + "GL89 6471 0123 4567 89\n" + "DO22 ACAU 0000 0000 0001 2345 6789\n" + "EG80 0002 0001 5678 9012 3451 8000 2\n" + "GE60 NB00 0000 0123 4567 89\n" + "GT20 AGRO 0000 0000 0012 3456 7890\n" + "IL17 0108 0000 0001 2612 345\n" + "JO71 CBJO 0000 0000 0000 1234 5678 90\n" + "KZ56 3190 0000 1234 4567 \n" + "XK05 1212 0123 4567 8906 \n" + "KW81 CBKU 0000 0000 0000 1234 5601 01\n" + "LB92 0007 0000 0000 1231 2345 6123\n" + "MK07 2000 0278 5123 453\n" + "MR13 0002 0001 0100 0012 3456 753\n" + "MU43 BOMM 0101 1234 5678 9101 000M UR\n" + "MD21 EX00 0000 0000 0123 4567\n" + "ME25 5050 0001 2345 6789 51\n" + "PK36 SCBL 0000 0011 2345 6702\n" + "PS92 PALS 0000 0000 0400 1234 5670 2\n" + "QA54 QNBA 0000 0000 0000 6931 2345 6\n" + "LC14 BOSL 1234 5678 9012 3456 7890 1234\n" + "ST23 0002 0000 0289 3557 1014 8\n" + "SA44 2000 0001 2345 6789 1234\n" + "RS35 1050 0812 3123 1231 73\n" + "TL38 0010 0123 4567 8910 106\n" + "TN59 0401 8104 0049 4271 2345\n" + "TR32 0010 0099 9990 1234 5678 90\n" + "AE46 0090 0000 0012 3456 789\n" + "VG21 PACG 0000 0001 2345 6789\n" + "UA90 3052 9929 9000 4149 1234 5678 9\n" + "SC52 BAHL 0103 1234 5678 9012 3456 USD\n" + "IQ20 CBIQ 8618 0010 1010 500\n" + "BY86 AKBB 1010 0000 0029 6600 0000\n" + "SV43 ACAT 0000 0000 0000 0012 3123\n" + "LY38 0210 0100 0000 1234 5678 9\n" + "SD88 1112 3456 7890 12 "; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); 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