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

Substitution

Processing...

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"(PA - REAL PARQUE(?s)(.*)mon\n999(?#Limpa o cabeçalho do Slip))" test_str = ("PA - REAL PARQUE CIA BRASILEIRA DE DISTRIBUICAO AV. MAJOR SYLVIO DE M PADILHA, 13000 REAL PARQUE, SAO PAULO - SP\n" "209 GIA) 29 78093 - ETTUATO Me 02:16\n" "anom\n" "CNPJ 47.508.411/0617-08|\n" "IE 116.804.053.119 IM 0000.000.000.000\n" "Extrato No. 105102 CUPOM FISCAL ELETRÔNICO - SAT\n" "CPF/CNPJ do Consumidor: Razão Social/Hone:\n" "# I COD DESC ! QTD I UN I VL UN R$ 1 (ULTR R$)* 1 VL ITEM R$\n" "wa Sasadas\n" "mon\n" "999\n" "001 00000004550280 ARROZ AG PRATO F IKG 1 UN X 3,69 (0,15) 3,69 002 00000000029995 PEITO SEARA kg 0,908 KG X 11,99 (1,77) 10,89 003 00000000029995 PEITO SEARA kg 0,866 KG X 8,99 (1,26) 7,79 004 00000001182380 QA GRA LGH BA QUI800 1 UN X 14.90 (0,83) desconto sobre item 005 00000001182380 QA GRA LGH BA QUI800 1 UN X 14,90 (1,67) 006 00000001184166 FILE CX SOB ATN NAT 1 UN X 15,99 (2,59) 15,99 007 00000001005372 TQ IOG DES AMO C/ALE 1 UN X 9,59 (1,07) 9,59 008 00000001005372 TQ IOG DES ANO C/ALE 1 UN X 9,59 (1.07) 9.59 009 00000000219631 MAMAO FORMOSA KG 2 KG X 6,99 (2,48) desconto sobre iten 010 00000001090888 CHUCHU 500G 1 UN X 3,69 (0.82) 011 00000001090208 ABOBRINHA ITALIA 400 1 UN X 4,49 (0,89) desconto sobre iten 012 00000000102056 QJ PARMESAO LA SEREN 0,228 KG X 49,90 (2,94) 013 00000000102056 QJ PARMESAO LA SEREN 0,230 KG X 49,90 (2,96) 014 00000006277901 ALFACE M R 150G 1 UN X 4,99 (1,11) 015 00000006277901 ALFACE M R 150G 1 UN X 4,99 (1,11) 4,99 016 00000000250399 BANANA NANICA GNEL K 1,245 KG X 4,99 (0,96)\n" "6,21 desconto sobre iten\n" "-1,87 | 017 00000000357593 TOMATE ITALIANO kg 1,590 KG X 6,59 (2,33)\n" "10,48 018 00000001166247 QA NOZES S CASCA 140 1 UN X 19,99 (4,87)\n" "19,99 019 00000004911166 QA LARANJA PERA 3KG 1 UN X 13,99 (2.33) 13,99 desconto sobre item\n" "-3,50 Total bruto de itens\n" "193,01 Total de descontos/acréscimos sobre item\n" "-16,12 TOTAL R$\n" "176,89 CARTAOON\n" "176,89\n" "4,99\n" "Comete crime quen sonega\n" "OBSERVACOES DO CONTRIBUINTE DataMov: 20190821 Loja: 1299 Terminal: 057 NCupom: 000149191 Valor aproximado dos tributos deste cupom R$\n" "33.21 (conforme Lei Fed 12.741/2012)\n" "3519 0847 5084 1106 1708 5900 0291 4731 0510 2996 1014\n") subst = "" # You can manually specify the number of replacements by changing the 4th argument result = re.sub(regex, subst, test_str, 0, re.MULTILINE) if result: print (result) # 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