Regular Expressions 101

Save & Share

  • Regex Version: ver. 1
  • 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

/
/
gx

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 = " (?(DEFINE)\n" + " (?<sep> [ -]?)\n" + " )\n" + " (?<!\\d)(?:\n" + " \\d{4} (?&sep) \\d{4} (?&sep) \\d{4} (?&sep) \\d{4} # 16 digits\n" + " | \\d{3} (?&sep) \\d{3} (?&sep) \\d{3} (?&sep) \\d (?&sep) \\d{3} # 13 digits\n" + " | \\d{4} (?&sep) \\d{6} (?&sep) \\d{4} # 14 digits\n" + " | \\d{4} (?&sep) \\d{6} (?&sep) \\d{5} # 15 digit card\n" + " )(?!\\d)"; final String string = "4111111111111111\n" + "4111 1111 1111 1111\n" + "4111-1111-1111-1111\n\n" + "American Express\n\n" + "378282246310005\n\n" + "American Express\n\n" + "371449635398431\n\n" + "American Express Corporate\n\n" + "378734493671000\n\n" + "Australian BankCard\n\n" + "5610591081018250\n\n" + "Diners Club\n\n" + "30569309025904\n\n" + "Diners Club\n\n" + "38520000023237\n\n" + "Discover\n\n" + "6011111111111117\n\n" + "Discover\n\n" + "6011000990139424\n\n" + "JCB\n\n" + "3530111333300000\n\n" + "JCB\n\n" + "3566002020360505\n\n" + "MasterCard\n\n" + "5555555555554444\n\n" + "MasterCard\n\n" + "5105105105105100\n\n" + "Visa\n\n" + "4111111111111111\n\n" + "Visa\n\n" + "4012888888881881\n\n" + "Visa\n\n" + "4222222222222\n\n" + "Note : Even though this number has a different character count than the other test numbers, it is the correct and functional number.\n\n" + "Processor-specific Cards\n\n" + "Dankort (PBS)\n\n" + "76009244561\n\n" + "Dankort (PBS)\n\n" + "5019717010103742\n\n" + "Switch/Solo (Paymentech)\n\n" + "6331101999990016\n\n" + " Visa\n\n" + "4929208147724685\n" + "4539261278252432\n" + "4916790209546242\n" + "4556229836495866\n" + "4556624948936262\n" + " Mastercard\n\n" + "5527513721190671\n" + "5427136938547169\n" + "5501820872619287\n" + "5106667846492187\n" + "5348965176175440\n" + " Discover\n\n" + "6011692621234093\n" + "6011505674384294\n" + "6011686367026051\n" + "6011805238158246\n" + "6011056229327552\n" + " American Express\n\n" + "370831029267044\n" + "345838860105292\n" + "346823285239073\n" + "370750517718351\n" + "340533441239380\n\n\n\n" + "PHONE NUMBERS:\n\n" + "Local (Canada)\n" + "613-555-0104\n" + "613-555-0141\n" + "613-555-0159\n" + "613-555-0130\n" + "613-555-0163\n" + "613-555-0137\n" + "International (Canada)\n" + "+1-613-555-0104\n" + "+1-613-555-0141\n" + "+1-613-555-0159\n" + "+1-613-555-0130\n" + "+1-613-555-0163\n" + "+1-613-555-0137"; final Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS); 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