Regular Expressions 101

Save & Share

  • Regex Version: ver. 1
  • Update Regex
    ctrl+⇧+s
  • Save new Regex
    ctrl+s
  • Add to Community Library

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"\b\d{7}\s+([A-Za-z].*?)\s+[TNGM]\b" test_str = ("\n" "['9076968 ADT 10mg 60comp 22CN014A T E1 059366 5 2,72 1,97 1,56 0,0 0,01 6 1,57 7,85', '9076943 ADT 25mg 60comp 22CN061A T E1 059366 10 3,91 3,09 2,60 0,0 0,01 6 2,61 26,10', '3506888 Aerius 5mg 20comp W010992 T E1 094546 5 4,99 4,11 3,53 10,0 0,02 6 3,20 16,00', '9046755 Aldactone 25mg 60comp B28191 G E1 084399 10 5,42 4,51 3,90 22,0 0,02 6 3,06 30,60', '5282132 Aranka MG 3mg+0,03mg 63comp T21521A G E2 087961 5 8,22 6,51 5,47 12,5 0,03 6 4,82 24,10', '6589168 Arnidol Gel Stick 15ml S-02 G NETT 054786 5 5,80 16,0 0,00 23 4,87 24,35', '5260542 Atorvastatina Azevedos MG 10mg 56comp 11400 T E1 094546 10 3,76 2,94 2,46 55,0 0,01 6 1,12 11,20', '5260559 Atorvastatina Azevedos MG 20mg 28comp 20515 T E1 059366 20 3,57 2,76 2,29 55,0 0,01 6 1,04 20,80', '5260575 Atorvastatina Azevedos MG 40mg 28comp 20516 T E1 059366 10 4,46 3,61 3,07 55,0 0,02 6 1,40 14,00', '5629506 Atozet 10mg+20mg 30comp W016401 N E5 093541 5 41,63 34,59 29,72 0,0 0,16 6 29,88 149,40', '7377390 Atyflor 10saq 124011 G NETT 087961 5 8,25 14,3 0,00 23 7,07 35,35', '2003093 Bebegel Gel Retal 6un 2206EA M NETT 024839 5 4,00 0,0 0,00 6 4,00 20,00', '8435701 Betadine Solucao Cutanea 125ml 326893 M NETT 084780 10 4,20 0,0 0,00 6 4,20 42,00', '2869584 Betamox Plus 875mg+125mg 16comp R017905R T E1 093541 30 6,34 5,39 4,71 60,0 0,02 6 1,90 57,00', '8184812 Betnovate 1mg/g Pomada 30g S63C N E1 022851 5 3,46 2,66 2,20 0,0 0,01 6 2,21 11,05', '2184992 Biloban 40mg 60comp rev R002315R T E2 059366 10 9,57 7,44 6,32 10,0 0,04 6 5,73 57,30', '5065487 Bisoprolol Sandoz MG 5mg 56comp LX8098 N E1 022851 5 5,01 4,13 3,55 0,0 0,02 6 3,57 17,85', '5138276 Buprenorfina Azevedos MG 8mg 7comp (P) 22E16 T E3 087485 30 11,15 8,83 7,42 5,0 0,04 6 7,09 212,70', '3126489 Calcitab 1500mg 60comp EQ22502 N E1 054786 5 6,29 5,34 4,66 0,0 0,02 6 4,68 23,40', '9729509 Cartia 100mg 28comp 20015 G E1 022851 30 5,41 4,13 3,55 45,0 0,02 6 1,97 59,10', '5037288 Ciprofloxacina Azevedos MG 500mg 16comp 11496 T E3 054786 5 10,87 8,57 7,18 70,0 0,04 6 2,19 10,95', '5273487 Co-Diovan Forte 160mg+25mg 28comp TRM93 N E2 022851 5 8,10 6,40 5,37 0,0 0,03 6 5,40 27,00', '8287607 Cordarone 200 mg x 60 Comprimidos 2R362 N E3 022851 5 11,36 9,03 7,61 0,0 0,04 6 7,65 38,25', '5440284 Coversyl 5mg 30comp rev 711191 N E1 022851 10 6,47 5,52 4,83 0,0 0,02 6 4,85 48,50', '5627781 Cozaar Plus 100mg + 12,5mg 28comp W020945 T E2 054786 5 7,69 6,01 5,01 9,0 0,03 6 4,59 22,95'") 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