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

r"
"
gm

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 = "(?:[1-9]\\d{5})(?:(?:1[89]\\d{2}|2\\d{3})(?:0[1-9]|1[012])(?:0[1-9]|[12][0-9]|3[01]))\\d{2}(?:\\d)(?:[0-9xX])"; final String string = "身份证号码\n" + "220602198308180937\n" + "320922198308202417\n" + "370285197906270016\n" + "32021119840313382X\n" + "310112197711256439\n" + "330206198111033414\n" + "140222198208261012\n" + "320621198110133038\n" + "320922197201306326\n" + "350783198801037037\n" + "532424198011100619\n" + "610103197502261615\n" + "342901198511180253\n" + "320203198112191851\n" + "32102419760102527X\n" + "340321198909215015\n" + "61272719820421604X\n" + "130681198501122628\n" + "320721198311184654\n" + "320982198402052773\n" + "230103198112296824\n" + "612521197209294238\n" + "340322198211124628\n" + "232302198202072716\n" + "23900519840128004X\n" + "450324198606101955\n" + "320623197705206994\n" + "330721197912252912\n" + "422431197609140715\n" + "320402197809293639\n" + "413027198005018417\n" + "430204198210061059\n" + "330283198308283719\n" + "342122198112244115\n" + "320222198002106926\n" + "229005197604121210\n" + "420381198307301211\n" + "320211198101063854\n" + "320525197811043014\n" + "370105197311092911\n" + "320204196207312019\n" + "420922198612278233\n" + "210902198112191014\n" + "321028197207063011\n" + "620102198006205817\n" + "32102019691015063X\n" + "412722198204228711\n" + "420684198402135034\n" + "321183198506015110\n" + "610404198210091033\n" + "440106197508261859\n" + "429006198808130950\n" + "360502197511295618\n" + "320683198510188619\n" + "320219198605140770\n" + "320219198209093538\n" + "330327198002070811\n" + "44128219821227341X\n" + "321283198209070434\n" + "420822198405245513\n" + "410728198507122654\n" + "433101198202130518\n" + "413024198207116710\n" + "350322198110205172\n" + "44010519831204579X\n" + "342921198109241711\n" + "420116198411142419\n" + "370602196904062113\n" + "370782198102132454\n" + "511028197411095112\n" + "321082197201170631\n" + "352202198306246938\n" + "320324197705061617\n" + "132629197409152914\n" + "440105198206030041\n" + "320722197810221618\n" + "320211197901084114\n" + "360622197711247013\n" + "340104197910101574\n" + "330226198505170038\n" + "310106196311130819\n" + "230122198706203516\n" + "120104197012266317\n" + "320682198911202219\n" + "320682198210254374\n" + "413027198109260013\n" + "142223198406068414\n" + "340824198411076034\n" + "320921198304260672\n" + "360403198104252413\n" + "310104196604100434\n" + "441622198412112579\n" + "360782198410113536\n" + "36078219851015081X\n" + "320405198210132517\n" + "370682198809305026\n" + "110105197512049532\n" + "412824198703102249\n" + "320203198105212511\n" + "370503197910202931\n" + "41012319770528441X\n" + "222424198110230248\n" + "370682198510128133\n" + "340521198311070086\n" + "340321198907061518\n" + "320204198307093529\n" + "321121197710231028\n" + "230702198201190518\n" + "320281198808245271\n" + "130427198510223114\n" + "310107198101104431\n" + "32068419800618341X"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); 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