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

/
/
msu

Test String

Code Generator

Generated Code

const regex = /(?=(?:""\s+?Controle\s*Fiscal[\w\W]+?)?""\s+(?<NomeUc>[\w\W]+?)$(?:.*?)""\s+?(?<Cnpj>\d{14})\s*(?:.*?)(?<MesReferencia>\d{2}\/\d{4})(?:.*?)(?<DtEmissao>\d{2}\/\d{2}\/\d{4})(?:.*?)\d{2}\/\d{2}\/\d{4}\s+(?<NumFatura>\d+)\s+(?:.*?)(?<ExternalCode>\d+-\d+)\s+(?:.*?)(?<DtVencimento>\d{2}\/\d{2}\/\d{4})\s+(?:.*?)(?<VlrTotalFatura>[0-9\.]+,?\d*)(?:.*?)Consta\snesta\sfatura\s*R.\s(?<VlrPisCofins>[0-9\.]+,?\d*)\sreferentes\sa\s(?:.*?)(?<AliqPis>[0-9\.]+,?\d*)%\sde\sPIS\se\s(?:.*?)(?<AliqCofins>[0-9\.]+,?\d*)%\sde\sCOFINS(?:.*?)\)\s+\d+\s+(?:\d+)?\s+(?<VlrBaseIcms>[0-9\.]+,?\d*)(?:.*?)""\s+(?<AliqIcms>\d+)\s+(?:.*?)^(?<DContratadaFp>[0-9\.]+,?\d*)\s+(?:.*?)[0-9\.]+,?\d*\s+(?<VlrIcms>[0-9\.]+,?\d*)\s+(?:.*?)""\s+(?<DataLeituraAtual>\d{2}\/\d{2}\/\d{4})\s+(?:.*?)(?<DataLeituraAnterior>\d{2}\/\d{2}\/\d{4})\s+(?:.*?)""\s+(?<LeituraAtualCFp>[0-9\.]+,?\d*)\s+(?:.*?)(?<LeituraAnteriorCFp>[0-9\.]+,?\d*)\s+(?:.*?)(?<ConstanteConsumoFp>[0-9\.]+,?\d*)\s+(?:.*?)(?<CMedidoFp>[0-9\.]+,?\d*)\s+(?:.*?)(?<LeituraAtualCP>[0-9\.]+,?\d*)\s+(?:.*?)(?<LeituraAnteriorCP>[0-9\.]+,?\d*)\s+(?:.*?)(?<ConstanteConsumoP>[0-9\.]+,?\d*)\s+(?:.*?)(?<CMedidoP>[0-9\.]+,?\d*)(?:.*?))/msu; // Alternative syntax using RegExp constructor // const regex = new RegExp('(?=(?:""\\s+?Controle\\s*Fiscal[\\w\\W]+?)?""\\s+(?<NomeUc>[\\w\\W]+?)$(?:.*?)""\\s+?(?<Cnpj>\\d{14})\\s*(?:.*?)(?<MesReferencia>\\d{2}\\\/\\d{4})(?:.*?)(?<DtEmissao>\\d{2}\\\/\\d{2}\\\/\\d{4})(?:.*?)\\d{2}\\\/\\d{2}\\\/\\d{4}\\s+(?<NumFatura>\\d+)\\s+(?:.*?)(?<ExternalCode>\\d+-\\d+)\\s+(?:.*?)(?<DtVencimento>\\d{2}\\\/\\d{2}\\\/\\d{4})\\s+(?:.*?)(?<VlrTotalFatura>[0-9\\.]+,?\\d*)(?:.*?)Consta\\snesta\\sfatura\\s*R.\\s(?<VlrPisCofins>[0-9\\.]+,?\\d*)\\sreferentes\\sa\\s(?:.*?)(?<AliqPis>[0-9\\.]+,?\\d*)%\\sde\\sPIS\\se\\s(?:.*?)(?<AliqCofins>[0-9\\.]+,?\\d*)%\\sde\\sCOFINS(?:.*?)\\)\\s+\\d+\\s+(?:\\d+)?\\s+(?<VlrBaseIcms>[0-9\\.]+,?\\d*)(?:.*?)""\\s+(?<AliqIcms>\\d+)\\s+(?:.*?)^(?<DContratadaFp>[0-9\\.]+,?\\d*)\\s+(?:.*?)[0-9\\.]+,?\\d*\\s+(?<VlrIcms>[0-9\\.]+,?\\d*)\\s+(?:.*?)""\\s+(?<DataLeituraAtual>\\d{2}\\\/\\d{2}\\\/\\d{4})\\s+(?:.*?)(?<DataLeituraAnterior>\\d{2}\\\/\\d{2}\\\/\\d{4})\\s+(?:.*?)""\\s+(?<LeituraAtualCFp>[0-9\\.]+,?\\d*)\\s+(?:.*?)(?<LeituraAnteriorCFp>[0-9\\.]+,?\\d*)\\s+(?:.*?)(?<ConstanteConsumoFp>[0-9\\.]+,?\\d*)\\s+(?:.*?)(?<CMedidoFp>[0-9\\.]+,?\\d*)\\s+(?:.*?)(?<LeituraAtualCP>[0-9\\.]+,?\\d*)\\s+(?:.*?)(?<LeituraAnteriorCP>[0-9\\.]+,?\\d*)\\s+(?:.*?)(?<ConstanteConsumoP>[0-9\\.]+,?\\d*)\\s+(?:.*?)(?<CMedidoP>[0-9\\.]+,?\\d*)(?:.*?))', 'msu') const str = `"" Controle Fiscal: 0B3EADFCEE1EECF867DEBB24AB24C691 "" AGUAS DE NITEROI S/A "" ROD BR 101 KM 30,5, S/N DISTRITO CONSELHEIRO "" 02150336000166 0704097 "" 86 41570091 0108-8 "" FAZ NOVA GAIVOT 12/2019 "" CAMPOS DOS GOYTACAZES 20/12/2019 "" 28100-000 24/12/2019 4421 Segunda Via 7889020-9 16/01/2020 2.447,37 Cons Fat Ponta 814,00 kWh2,781802.264,38 Cons Fat F Ponta 8568,00 kWh 0,541594.640,34 Dem Fat F Ponta 73,00 kW31,790002.320,67 Ajuste -4.833,30 Iluminacao Publica 126,69 ENERGIA CONSUMIDA GD BAND VERDE 6.015,70 Cred Energia Injetada G. Distribuida -6.015,70 0,00 Devolucao ICMS -2.071,41 "" 0,00 VR ADIC BAND AMARELA DO MES ( R\$ 347,52 ) Consta nesta fatura R\$ 533,22 referentes a 1,04% de PIS e 4,74% de COFINS (Art. 10 Res. 298/2006-ANEEL e Leis N. 10.637/02 e 10.883/03) 33375978 9.225,39 "" 30 1.000,00 0,00 2.767,61 Tipo de Faturamento: Normal 19/12/2019 19/11/2019 19/12/2019 19/11/2019 357,00 255,00 84,000 8568,00 3555,00 2586,00 0,840 814,00 63,00 41,00 3,360 73,00 46,00 30,00 3,360 53,00 1,00 1,00 84,000 0,00 0,00 0,00 0,840 0,00 219,00 140,00 0,840 66,36 137,00 89,00 0,840 40,32 "" 13,80 kV IGOR MACHADO "" 20/01/2020 0 0 0 0 0 THS VERDE A4 C/DES15Telefone:2716-1634 0 0 0 0 14,49 kV 0 0 E-mail:igor.santos@enel.com10/2019 Servico Publico "" 12,83 kV 0,00% 0 0 Banco Itaú SA 341-7 34191.09008 30346.832931 83707.520009 1 81360000244737 PAGÁVEL PREFERENCIALMENTE NO ITAÚ Contra-apresentação AMPLA Energia e Serviços S.A. 2938/37075-2 20/12/2019 4421 DP N 175/00303468-3 "" 175 2.447,37 Pagamentos efetuados após o dia 16/01/2020, incorrerão encargos financeiros de mora. Os encargos financeiros citados, serão cobrados em faturas posteriores. Regime Especial - processo no E-04/142.091/97 AGUAS DE NITEROI S/A ROD BR 101 KM 30,5, S/N DISTRITO CONSELHEIRO "" CONTA PAGA `; // Reset `lastIndex` if this regex is defined globally // regex.lastIndex = 0; let m; if ((m = regex.exec(str)) !== null) { // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); }

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 JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions