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

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"^.*\(((?:RR|RI|PC|PR|PA)R\d{3}){0,1}\) \[([1-2]\d{7}@\d{6})\]\.(( <<.*>>)|( (?!<<).+)){0,1}$" test_str = ("None (PRR001) [11111111@111111]. <<freetext>>\n" "None (RIR001) [11111111@111111]. <<freetext>>\n" "None (RRR001) [11111111@111111]. <<freetext>>\n" "Prescribing Error (PCR001) [11111111@111111]. <<freetext>>\n" "None (PAR001) [11111111@111111]. <<freetext>>\n\n" "None (RIR001) [11111111@111111]. a\n" "None (RIR001) [11111111@111111]. <a>>\n\n" "--GOOD Leading and trailing spaces are ignored\n" "None (PRR001) [11111111@111111]. << freetext >>\n\n" "--GOOD One element missing\n" "(PRR001) [11111111@111111]. <<freetext>>\n" "(PRR001) [11111111@111111]. <<freetext>>\n" "None () [11111111@111111]. <<freetext>>\n" "None (PRR001) [11111111@111111]. <<>>\n" "None (PRR001) [11111111@111111].\n\n" "--GOOD Freetext embedded <<>>. Leading and trailing spaces\n" "None (PRR001) [11111111@111111]. <<C<<B>>A>>\n\n" "--GOOD Old Reason format with correct event type.\n" "Prescription Reprint <<>>\n" "Prescription Reprint <<chewed up>>\n\n" "--GOOD Old Reason format with with freetext embedded <<>>\n" "Prescription Reprint <<C<<B>>A>>\n\n" "--GOOD Longer than 400\n" "None (PRR001) [11111111@111111]. <<Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ultrices orci non leo volutpat, at sagittis dolor cursus. Duis ullamcorper laoreet scelerisque. Donec eu nulla nec erat eleifend consectetur. Donec tincidunt, risus id imperdiet consectetur, eros mauris pharetra purus, in rhoncus diam massa ac elit. Suspendisse eleifend erat vitae cras amet.>>\n\n" "--GOOD <400 freetext > 350\n" "None (PRR001) [11111111@111111]. <<Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed condimentum, erat nec rhoncus porta, urna sem laoreet elit, sed tempus augue erat nec odio. Morbi facilisis porttitor dolor sodales blandit. Phasellus lobortis ligula sit amet elit sagittis, eu ultrices mi aliquet. Curabitur a odio mauris. Nam quis ullamcorper elit. In dignissim ac nullam.>>\n\n" "--GOOD <CR><LF><TAB>\n" "None (PRR001) [11111111@111111]. <<EA\\r\\nB\\t\\tC>>\n\n" "--BAD Prove only certain codes looked at\n" "None (PRR001) [11111111@111111]. <<freetext>>\n\n" "--BAD One element missing\n" "None (PRR001) []. <<freetext>>\n\n" "--BAD Code not RRRnnn,RIRnnn,PCRnnn,PRRnnn or PARnnn\n" "None (PIR001) [11111111@111111].\n" "None (RCR001) [11111111@111111].\n" "None (RAR001) [11111111@111111].\n" "None (RIR00A) [11111111@111111].\n" "None (RIRA01) [11111111@111111].\n" "None (RIR0A1) [11111111@111111].\n" "None (RIR 01) [11111111@111111].\n" "None (RIR0 1) [11111111@111111].\n" "None (RIR00 ) [11111111@111111].\n" "None (RIR001 ) [11111111@111111].\n" "None (RIR0001) [11111111@111111].\n" "None ( RIR001) [11111111@111111].\n" "None (RIR001 [11111111@111111].\n" "None RIR001) [11111111@111111].\n" "None (RIR01) [11111111@111111].\n" "None (EVE001) [11111111@111111].\n\n" "--BAD Date time not \\[([1-2]\\d{7}@\\d{6})\\]\n" "None (RIR001) 11111111@111111].\n" "None (RIR001) [11111111@111111.\n" "None (RIR001) [1111111@111111].\n" "None (RIR001) [111111111@111111].\n" "None (RIR001) [11111111@11111].\n" "None (RIR001) [11111111@1111111].\n" "None (RIR001) [91111111@111111].\n" "None (RIR001) [11111111@11A111].\n" "None (RIR001) [111A1111@111111].\n" "None (RIR001) [11111111x111111].\n" "None (RIR001) [11111111111111].\n" "None (RIR001) [].\n" "None (RIR001).\n\n" "--BAD Reason text, code, date time with no .\n" "None (RIR001) [11111111@111111]\n\n" "--BAD Invalid freetext ( <<.*>>){0,1}\n" "None (RIR001) [11111111@111111].a\n" "None (RIR001) [11111111@111111]. \n" "None (RIR001) [11111111@111111]. <<a>\n" "None (RIR001) [11111111@111111]. <<>> a\n" "None (PRR001) [11111111@111111]. <<C<<B>>A\n" "None (PRR001) [11111111@111111]. <<venalink\n\n" "--BAD Old Reason format with wrong event type.\n" "Prescription Reprint\n" "Prescription Reprint\n" "Prescription Reprint\n" "Prescription Reprint\n" "Prescription Reprint\n" "Prescription Reprint <<>>\n" "Prescription Reprint <<>>\n" "Prescription Reprint <<>>\n" "Prescription Reprint <<>>\n" "Prescription Reprint <<chewed up>>\n" "Prescription Reprint <<chewed up>>\n" "Prescription Reprint <<chewed up>>\n" "Prescription Reprint <<chewed up>>\n" "Prescription Reprint <<C<<B>>A\n" "Prescription Reprint <<C<<B>>A\n" "Prescription Reprint <<C<<B>>A\n" "Prescription Reprint <<C<<B>>A\n\n" "--BAD Old Reason format with with freetext embedded <<>> and no Trailing >>\n" "Prescription Reprint <<C<<B>>A\n\n" "--OTHER BAD EXAMPLES\n\n" "Existing letter file 'P:\\WORDPROC\\CLINCORR\\0046O000.CLI' deleted when adding letter\n\n" "Vision 2.00 Install successful on Computername: RECEPTION for filesystem: Live System - Live\n\n" "Typed in name: MD\n\n" "Mode=LEGACY;Token Id=AQIC5wM2LY4SfcyXAQB0EBD9MWbdEKQjnCGKw8iNj6Gu3FU=;RoleProfile=339601626031;Role=R1740\n\n" "Prescription Reprint <<chewed up>>\n\n" "Mode=CACHED PROFILE;Token Id=;RoleProfile=;Role=,User=SYS\n\n" "14.9\n\n" "Vision 22.00.0004 Install successful on Computername: CON06 for filesystem: Live System - Live\n\n" "ÃÂ?Â?Â?Â?Â?Â<ÿUÂ<ìƒì(SVWÂ?zøÂ?Â\"[K\n\n" "Existing letter file 'P:\\WORDPROC\\CLINCORR\\00311000.CLI' deleted when adding letter\n\n" "Quarter 1 2000\n") matches = re.finditer(regex, test_str, re.MULTILINE) for matchNum, match in enumerate(matches, start=1): print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group())) for groupNum in range(0, len(match.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.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