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

$re = '/(?:[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])/m'; $str = '身份证号码 220602198308180937 320922198308202417 370285197906270016 32021119840313382X 310112197711256439 330206198111033414 140222198208261012 320621198110133038 320922197201306326 350783198801037037 532424198011100619 610103197502261615 342901198511180253 320203198112191851 32102419760102527X 340321198909215015 61272719820421604X 130681198501122628 320721198311184654 320982198402052773 230103198112296824 612521197209294238 340322198211124628 232302198202072716 23900519840128004X 450324198606101955 320623197705206994 330721197912252912 422431197609140715 320402197809293639 413027198005018417 430204198210061059 330283198308283719 342122198112244115 320222198002106926 229005197604121210 420381198307301211 320211198101063854 320525197811043014 370105197311092911 320204196207312019 420922198612278233 210902198112191014 321028197207063011 620102198006205817 32102019691015063X 412722198204228711 420684198402135034 321183198506015110 610404198210091033 440106197508261859 429006198808130950 360502197511295618 320683198510188619 320219198605140770 320219198209093538 330327198002070811 44128219821227341X 321283198209070434 420822198405245513 410728198507122654 433101198202130518 413024198207116710 350322198110205172 44010519831204579X 342921198109241711 420116198411142419 370602196904062113 370782198102132454 511028197411095112 321082197201170631 352202198306246938 320324197705061617 132629197409152914 440105198206030041 320722197810221618 320211197901084114 360622197711247013 340104197910101574 330226198505170038 310106196311130819 230122198706203516 120104197012266317 320682198911202219 320682198210254374 413027198109260013 142223198406068414 340824198411076034 320921198304260672 360403198104252413 310104196604100434 441622198412112579 360782198410113536 36078219851015081X 320405198210132517 370682198809305026 110105197512049532 412824198703102249 320203198105212511 370503197910202931 41012319770528441X 222424198110230248 370682198510128133 340521198311070086 340321198907061518 320204198307093529 321121197710231028 230702198201190518 320281198808245271 130427198510223114 310107198101104431 32068419800618341X'; 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