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

$re = '/^(?<an>[A-Z]{1,2}\d{10,11})\s+\b(?<pn>[\w\-\' ,\.]+?) (?<loc>\b[A-Z1-9][A-Z0-9\-–—]+) (?<rm>\b\w+ )?(?<bd>\b\w+ )?(?i:Primary Dx)/m'; $str = 'V12345678910 last, first MI D-321 D123 1 Primary Dx V12345678910 last, first I D-321 D123 1 Primary Dx V12345678910 last, first I Jr. D-321 D123 1 Primary Dx V12345678910 last, first I III D-321 D123 1 Primary Dx V12345678910 last, first I Jr. D-321 D123 1 Primary Dx V12345678910 last, first-first2 D-321 D123 Primary Dx V12345678910 l\'astlast2, first MI D-321 D123 Primary Dx V12345678910 last, first II D-321 D123 1 Primary Dx V12345678910 last-last2, first-first2 321 D123 1 Primary Dx V12345678910 last-last2, first-first2 I D-321 D123 1 Primary Dx V12345678910 last-last2, first-first2 I D-321 D123 Primary Dx V12345678910 last-last2, first-first2 I ED Primary Dx V12345678910 last-last2, first I ED Primary Dx V12345678910 last-last2, first-first2 ED PRIMARY DX V12345678910 LAST,FIRST FIRST ARU P102 1 Primary Dx V12345678910 last, first D321 D123 1 Primary Dx V12345678910 last, first-first2 first3 D321 Primary Dx V12345678910 last, first-first2 D-321 123 Primary Dx V12345678910 last, first-first2 D-321 123 Primary Dx V12345678910 last, first-first2 D-321 123 1 Primary Dx V12345678910 last, first-first2 D JIP LC A Primary Dx VN2345678910 last,first first2 D JIP Primary Dx These are a few assumptions made: First match is the Account Number named <an> 2nd match is the Patient name named <pn> There is 1 space between the end of the patient\'s name and the beginning of the Location. and between Location and Room, and between Room and Bed There will always be a Location (Room and Bed may or may not exist). 3rd match is the Location named <loc> Location may contain "-" No spaces in Location No spaces in Room No spaces in Bed 4th match is the Room named <rm> Room or Bed does not contain "-" The Room may or may not exist 5th match is the Bed named <bd> Bed can not exist without a Room Room can not exist without a Location Use Alt+0150 (en dash), Alt+0151 (em dash), or Alt+8722 (minus sign) using the numeric keypad. Alt+0150 (en dash) – Alt+0151 (em dash) — (minus sign) - '; 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