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

/
/

Test String

Code Generator

Generated Code

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"(Policy Number:\s*)([A-Z0-9]+)(?=\s*Plan Eff\. Date:)" test_str = "OBX|1|FT|ED Fin||\\.br\\Butler Memorial Hospital Emergency Department\\.br\\\\.br\\One Hospital Way\\.br\\\\.br\\Butler, PA, 16001\\.br\\\\.br\\7242836666\\.br\\\\.br\\Patient: UPMC TEST DOB: 1/1/1945\\.br\\\\.br\\MR #: 200115 Age/Gender: 70y F\\.br\\\\.br\\DOS: 11/27/2015 08:15 Acct #: 20005591\\.br\\\\.br\\Private Phys: ED Phys:\\.br\\\\.br\\CHIEF COMPLAINT: Enc. Type: ACUITY:\\.br\\\\.br\\******** Initial Acuity unassigned\\.br\\\\.br\\Insurance\\.br\\\\.br\\1. Insurer Details\\.br\\\\.br\\Name: Relationship: SELF\\.br\\\\.br\\SSN: Employer Name:\\.br\\\\.br\\Sex:\\.br\\\\.br\\Insurance Type: UPMC HEALTH PLAN Plan Id: CPUPMC\\.br\\\\.br\\Policy Number: DKJSLFLSF Plan Eff. Date:\\.br\\\\.br\\Group Name: Group Number:\\.br\\\\.br\\Company Name: UPMC HEALTH PLAN Priority: 1\\.br\\\\.br\\Billing Status: Verify Indicator:\\.br\\\\.br\\TRIAGE\\.br\\\\.br\\Arrival: Patient arrived walking via car/private vehicle from home accompanied\\.br\\by parent(s) < KAL1 11/27/2015 08:24>\\.br\\\\.br\\Pain Level: Patient's pain is a 2 using a Wong-Baker scale. < KAL1 11/27/2015\\.br\\08:25>\\.br\\\\.br\\Patient not currently taking any medications.\\.br\\< KAL1 11/27/2015 08:25>\\.br\\\\.br\\Medical and surgical history obtained. <KAL1 11/27/15 08:25 >\\.br\\\\.br\\Patient states no travel to West Africa nor had contact with any person that\\.br\\has traveled to West Africa in the 21 days before illness onset. <KAL1\\.br\\11/27/2015 08:24>\\.br\\\\.br\\ALLERGIES\\.br\\\\.br\\+---------------------+--------------------+--------+------------------------+\\.br\\\\F\\Allergen \\F\\Status \\F\\Comments\\F\\Updated \\F\\\\.br\\\\F\\Reaction \\F\\ \\F\\ \\F\\ \\F\\\\.br\\+---------------------+--------------------+--------+------------------------+\\.br\\\\F\\*No Known Allergies* \\F\\Confirmed or \\F\\ \\F\\<KAL1 Nov 27 2015 \\F\\\\.br\\\\F\\ \\F\\verified \\F\\ \\F\\8:25AM> \\F\\\\.br\\+---------------------+--------------------+--------+------------------------+\\.br\\Patient not currently taking any medications.\\.br\\< KAL1 11/27/2015 08:25>\\.br\\\\.br\\HOME MEDICATIONS\\.br\\\\.br\\+-----------------------+----------+------+--------+-------------------------+\\.br\\\\F\\Medication Name \\F\\Last Dose \\F\\Status\\F\\Comments\\F\\Updated \\F\\\\.br\\\\F\\Instructions \\F\\Taken Date\\F\\ \\F\\ \\F\\ \\F\\\\.br\\+-----------------------+----------+------+--------+-------------------------+\\.br\\\\F\\*Not Taking Home Meds* \\F\\ \\F\\Active\\F\\ \\F\\<KAL1 Nov 27 2015 8:25AM>\\F\\\\.br\\+-----------------------+----------+------+--------+-------------------------+\\.br\\PAST MEDICAL / SURGICAL / SOCIAL HISTORY\\.br\\\\.br\\Smoking Status: Never smoked\\.br\\< KAL1 11/27/2015 08:25>\\.br\\\\.br\\No relevant surgeries.\\.br\\< KAL1 11/27/2015 08:26>\\.br\\\\.br\\DISPOSITION\\.br\\\\.br\\Nursing\\.br\\\\.br\\Disposition is Discharged - Routine\\.br\\< KAL1 11/27/2015 08:26>\\.br\\\\.br\\Family not notified of patient's disposition. <KAL1 11/27/15 08:26 >\\.br\\\\.br\\------------------------------------------------------------------------------\\.br\\\\.br\\Bed Assignments:\\.br\\\\.br\\MainWR INU 11/27/2015 08:15\\.br\\\\.br\\Status Activity:\\.br\\\\.br\\With Triage RN INU 11/27/2015 08:15\\.br\\\\.br\\Released KAL1 11/27/2015 08:28\\.br\\\\.br\\Acuity Activity:\\.br\\\\.br\\Acuity unassigned INU 11/27/2015 08:15\\.br\\\\.br\\Chart Documented By:\\.br\\\\.br\\KAL1: KALLIE TEST SIMMONS\\.br\\\\.br\\Release Information:\\.br\\\\.br\\Patient released 11/27/2015 08:28\\.br\\\\.br\\Released by KALLIE TEST SIMMONS\\.br\\\\.br\\Printed By User N. Interface on 11/27/2015 8:30 AM\\.br\\\\.br\\Medical Chart\\.br\\\\.br\\||||||S|" matches = re.search(regex, test_str) if matches: print ("Match was found at {start}-{end}: {match}".format(start = matches.start(), end = matches.end(), match = matches.group())) for groupNum in range(0, len(matches.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = matches.start(groupNum), end = matches.end(groupNum), group = matches.group(groupNum))) # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

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 Python, please visit: https://docs.python.org/3/library/re.html