Regular Expressions 101

Save & Manage Regex

  • Current Version: 1
  • Save & Share
  • Community Library

Flavor

  • PCRE2 (PHP)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java
  • .NET 7.0 (C#)
  • Rust
  • PCRE (Legacy)
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests
Sponsors
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
Processing...

Test String

Code Generator

Generated Code

import re regex = re.compile(r"(\d{1,2})(\s)(\xA0\xA0)(\s)(\d{1,7})(\s)(\w.+)(\s)(\d{1,3}\.\d{1,3}|\d{1,3})(\s)(\w\w)(\sx\s)(\d{1,3}\,\d{1,2})(\s)(\(\d\)|\(\d{1,3}\.\d{1,2}\))(( (0)(\s)(\d{1,3}\,\d\d)|(\s)(\n)(\xA0\xA0\xA0\xA0\sDesconto\s)([0-9]{1,3}\,[0-9]{2})(\n)(-\s[0-9]{1,2}\,[0-9]{2})))", flags=re.MULTILINE) test_str = ("REAL PARQUE \n" "CNPJ 47.508.411/0617-08 IE 116804053119 \n" "CIA BRASILEIRA DE DISTRIBUICAO \n" "AV MAGALHAES DE CASTRO 6118 \n" "SAO PAULO - SP\n" "CUPOM FISCAL ELETRÔNICO - SAT\n" "CPF/CNPJ do Consumidor: 007.270.248-68 \n" "Razão Social/Nome: AGUINALDO BERLOFFE\n" "#   |   COD   |   DESC   |   QTD   |   UN   |   VL UN R$   |   (VLTR R$)*   | VL ITEM R$\n" "1    7047411 LEITE NIN NESTLE 1LT 3 UN x 4,69 (14.07) \n" "     Desconto 14,07\n" "- 2,70\n" ",\n" "2    0790642 CERV HEINEKEN 350ML 12 UN x 3,49 (41.88) 0 41,88\n" ",\n" "3    4231486 LT SEMI DES NINHO 1L 1 UN x 4,69 (4.69) \n" "     Desconto 4,69\n" "- 0,90\n" ",\n" "4    1103188 VODKA VORUS 1L 1 UN x 25,99 (25.99) 0 25,99\n" ",\n" "5    1049831 ARROZ INTEGRAL PF 1K 2 UN x 4,09 (8.18) 0 8,18\n" ",\n" "6    6009229 QA ARROZ T1 1KG 1 UN x 2,99 (2.99) 0 2,99\n" ",\n" "7    3878682 TQ AGUA COCO TAEQ 1L 2 UN x 7,49 (14.98) 0 14,98\n" ",\n" "8    8788801 IOG.FAZ.BELA VISTA M 1 UN x 3,79 (3.79) 0 3,79\n" ",\n" "9    4299066 IOG B VISTA F V 500G 2 UN x 3,79 (7.58) 0 7,58\n" ",\n" "10    6803186 COGUMEL QUALITA 100G 2 UN x 5,99 (11.98) 0 11,98\n" ",\n" "11    5616947 KETCHUP HEINZ 397GR 1 UN x 8,99 (8.99) 0 8,99\n" ",\n" "12    1207692 BIFE COPA LOMBO 1KG 1 UN x 19,94 (19.94) 0 19,94\n" ",\n" "13    0076937 BACON DEF KG 0.298 KG x 35,49 (10.58) 0 10,58\n" ",\n" "14    0001311 PEITO FGO C/OSSO KG 0.972 KG x 9,99 (9.71) 0 9,71\n" ",\n" "15    0226998 PATINHO BIFES ATP 0.528 KG x 34,99 (18.47) 0 18,47\n" ",\n" "16    0226998 PATINHO BIFES ATP 0.54 KG x 34,99 (18.89) 0 18,89\n" ",\n" "17    0001311 PEITO FGO C/OSSO KG 0.914 KG x 9,99 (9.13) 0 9,13\n" ",\n" "18    1161632 BISNAGUIN PAN 300G 1 UN x 6,69 (6.69) 0 6,69\n" ",\n" "19    1197527 MAC SPAG LA MOLISANA 1 UN x 2,29 (2.29) 0 2,29\n" ",\n" "20    0787055 MAC REN SUP PEN 500G 1 UN x 6,59 (6.59) 0 6,59\n" ",\n" "21    2022369 GR DR FUSIL ADRIA 50 1 UN x 5,59 (5.59) 0 5,59\n" ",\n" "22    3197899 QA OVO VM GD LG C/10 2 UN x 11,90 (23.8) 0 23,80\n" ",\n" "23    1090871 QA CENOURA 500G 1 UN x 4,79 (4.79) 0 4,79\n" ",\n" "24    3209950 QA MILHO VERDE 700G 1 UN x 6,49 (6.49) 0 6,49\n" ",\n" "25    1091830 PIMENTAO VERM 300G 1 UN x 4,99 (4.99) 0 4,99\n" ",\n" "26    0287760 CEBOLA GRANEL KG 1.165 KG x 7,98 (9.3) 0 9,30\n" ",\n" "27    0287753 BATATA GRANEL KG 1.47 KG x 7,98 (11.73) 0 11,73\n" ",\n" "28    1207899 QA NOZES SC NAC 140G 1 UN x 25,39 (25.39) 0 25,39\n" ",\n" "29    1090796 QA BERINJELA COMUM 1 UN x 4,39 (4.39) 0 4,39\n" ",\n" "30    0251280 KIWI IMP GNEL KG 0.525 KG x 21,99 (11.54) 0 11,54\n" ",\n" "31    5107452 CHUCHU VDE ESC 600G 1 UN x 2,69 (2.69) 0 2,69\n" ",\n" "32    0357593 TOMATE ITALIANO KG 1.04 KG x 7,69 (8) 0 8,00\n" ",\n" "33    4600039 QA CER ULT FLAK 300G 1 UN x 5,99 (5.99) \n" "     Desconto 5,99\n" "- 1,50\n" ",\n" "34    0337182 GENGIBRE GNEL KG 0.805 KG x 12,39 (9.97) 0 9,97\n" ",\n" "35    0227841 MANGA PALMER NAC KG 0.655 KG x 5,49 (3.6) 0 3,60\n" ",\n" "36    0247528 MACA FUJI KG 0.64 KG x 9,98 (6.39) 0 6,39\n" ",\n" "37    6072100 QA UVA VERM SS 500G 1 UN x 8,29 (8.29) 0 8,29\n" ",\n" "38    4333364 ALFACE AMERICANA 250 2 UN x 3,49 (6.98) 0 6,98\n" ",\n" "39    0219631 MAMAO FORMOSA KG 1.64 KG x 5,49 (9) 0 9,00\n" ",\n" "40    1177380 QA SALGAD BACON 100G 1 UN x 3,59 (3.59) 0 3,59\n" ",\n" "41    7533006 PAO 100 INT G CAST 1 UN x 8,99 (8.99) 0 8,99\n" ",\n" "42    1225200 QA BISC AVEI MEL170G 2 UN x 2,29 (4.58) 0 4,58\n" ",\n" "43    1121371 BISC CERE MAC/UV141G 2 UN x 3,39 (6.78) 0 6,78\n" ",\n" "44    1045921 QA SALG CEBOLA 100G 1 UN x 4,99 (4.99) 0 4,99\n" ",\n" "45    0049771 MORT BOL TR CER FTKG 0.128 KG x 39,90 (5.11) 0 5,11\n" ",\n" "46    0012775 PRES COZ SAD MAGRO 0.158 KG x 19,90 (3.14) 0 3,14\n" ",\n" "47    0013116 SALAME IT SAD SSS KG 0.104 KG x 69,99 (7.28) 0 7,28\n" ",\n" "48    0143745 QA QJ MUSS QA FAT 0.262 KG x 33,69 (8.83) \n" "     Desconto 8,83\n" "- 1,78\n" ",\n" "49    7094651 AMEND JAP YOKI 500G 1 UN x 9,59 (9.59) 0 9,59\n" ",\n" "50    5758203 PAO HOT DOG 200G 1 UN x 8,19 (8.19) 0 8,19\n" "TOTAL R$ 480,52\n" "Troco R$ 0,00\n" "OBSERVAÇÕES DO CONTRIBUINTE\n" "Datalov: 20200510 Loja: 1299 Terminal: 110 NCupon: 00281819\n" "Valor aproximado dos tributos deste cupom R$ 101,77\n" "(conforme Lei Fed. 12.741/2012)\n" "SAT No. 110 \n" "10/05/2020 10:57:23\n" "35200547508411061708651100000347431306165855") matches = regex.finditer(test_str) for match_num, match in enumerate(matches, start=1): print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}") for group_num, group in enumerate(match.groups(), start=1): print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")

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