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
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
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • 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
No Match

r"
"
g

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"\W\b(A[CLPM]|BA|CE|DF|GO|ES|M[ATSG]|P[ABREI]|R[JNSOR]|S[PCE]|TO)\W" test_str = (" Nota Fiscal- Série U - 118480294 Cód. Fiscal de operação: 5.255 FAT: 01-202042169950351-3\n\n\n\n" " 6381.27DC.9D51.9288.E2EA.43ED.CF4E.EEBD\n\n\n" " TELEFONICA BRASIL S.A.\n\n" " Av Gildo de Freitas, 296, Ap 00001\n" " Bairro: Vila Querencia\n" " 61547212\n" " Viamao - RS Fase: MONOFASICO\n" " CPJ: 02558157001720 Bandeira Vigente: Bandeira Verde\n" " Classe de Consumo Aneel: COMERCIAL, SERVICOS E OUTRAS ATIVIDADES - Servicos de Comunicacoes e Telecomunicacoes\n\n\n" " kWh\n" " Nº do medidor 33880914\n" " Fator de Multiplicação 1,000 1.343 kWh 07/2020 05/09/2020 1.042,25\n" " Leitura 16/07/2020 67179\n" " Leitura 16/06/2020 65836\n" " Consumo* 1343\n" " Consumo 1.343 0,764147 1.026,25\n" " *Consumo Lido Subtotal (R$) 1.026,25\n" " Lançamentos e Serviços\n" " Contrib.Ilum.Pub.Prefeitura 16,00\n" " Subtotal (R$) 16,00\n" " Perdas de Transformações (%): 0\n" " Fator de Potência: 1,00000\n" " Período Fiscal 01/07/2020\n" " Emissão 17/07/2020\n" " Apresentação 27/07/2020\n" " Próxima Leitura Prevista: 17/08/2020\n\n\n\n\n" " R$\n" " Energia 356,70\n" " Transmissao 58,91\n" " Distribuicao 149,26\n" " Enc. Setoriais 65,01\n" " Tributos 334,52\n" " Perdas 61,85\n\n\n\n\n" " Dias Consumo Diário\n" " 2020 JUL 30 1343 44,77\n" " JUN 32 1490 46,56\n" " MAI 30 1404 46,8\n" " ABR 30 1450 48,33\n" " MAR 32 1600 50\n" " FEV 29 1457 50,24\n" " JAN 30 1531 51,03\n" " 2019 DEZ 28 1431 51,11\n" " NOV 33 1644 49,82\n" " OUT 32 1628 50,88\n" " SET 30 1410 47\n" " AGO 30 1461 48,7 ICMS Base de Cálculo (R$) 1.026,25 aliquota 30% R$ 307,88\n" " JUL 32 1572 49,13 PIS/COFINS Conf. Res. ANEEL nº 234/2005 aliquota 2,5968% R$ 26,64\n\n\n\n" " Periodos Band.Tarif.: Verde:17/06-16/07\n\n\n\n\n" " UC: 61547212 FAT: 01-202042169950351-3\n" " Fatura de Energia Elétrica do Mês de Julho de 2020 Valor a pagar R$ 1.042,25\n\n\n" "Mês/Ano Vencimento NÃO RECEBER - DÉBITO AUTOMÁTICO - BANCO - 008 - AGÊNCIA - 2271\n" "07/2020 05/09/2020 Caso não ocorra o débito, utilize o código abaixo para pagamento\n" " 836400000102 422500060005 001012020424 169950351034\n" " 94410-330\n" " Viamao - RS\n" " Francisco Carvalho da Cunha, 177\n" "TELEFONICA BRASIL S.A. Agência de atendimento\n" "Av Gildo de Freitas, 296 - Ap 00001 CEEE\n\n" "Vila Querencia\n" "94410-300 - Viamao - RS\n" "Medidor: MD 33880914 - Apresentacao: 27/07/2020 - Secc/Loc/Etapa/Liv: 1806,1806,10,006692\n\n\n\n\n" "Indicadores de Continuidade do Fornecimento de Energia\n" " Padrão Realizado\n\n" "Conjunto: VIAMAO 1 Mensal Trimestral Anual Maio/2020\n" "DIC - Duração de Interrupção Individual (horas) 5,43 10,86 21,73 0,00\n" "FIC - Frequência de Interrupção Individual (vezes) 3,42 6,85 13,70 0,00\n" "DMIC - Duração Máxima de Interrupção Contínua (horas) 3,11 0,00\n" "DICRI - Duração da Interrupção Individual Dia Crítico (horas): 12,22\n" "EUSD - Valor Enc. Uso Sist. Distr (R$) 348,51\n" "TENSÃO NOMINAL: 220V - GRUPO B Limite Inferior: 202V Limite Superior: 231V\n" " ") matches = re.finditer(regex, test_str) 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