Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex - groups matching same pattern

I'm trying to regex groups matching the same pattern using C#. Here is a little example which I can't get to work.

I need to get all the strings between the single quotes (CodigoEmpresa, for example)

uses MainRecord, objErrorList, SysUtils, XMLMXMWebServiceReturn, objMainProcesso,
 objProcessoWS, objProcessaRelatorioQuickReport, QuickRpt, Forms,
 RBalanc, RBalancete, RBaCCMens, RBalaMensal, RBalaMensalCons,
 objcadcontabilidade, objContabilidadeValidacoes;

const
CODIGO_EMPRESA             = 'CodigoEmpresa';
ANO_MES                    = 'AnoMes';
RELATORIO_POR              = 'RelatorioPOR';
CONTA_INI                  = 'ContaIni';
CONTA_FIM                  = 'ContaFim';
GRAU_CONTA                 = 'GrauConta';
CCUSTOS_INI                = 'CCustosIni';
CCUSTOS_FIM                = 'CCustosFim';
GRAU_CCUSTOS               = 'GrauCCustos';
DETALHAR_CONSOLIDADO       = 'DetalharConsolidado';
DESCONSIDERAR_ENCERRAMENTO = 'DesconsiderarEncerramento';
QUEBRA_CCUSTO              = 'QuebraCCusto';
CONTAS_SEM_MOVIMENTO       = 'ContasSemMovimento';
CODIGO_ALTERNATIVO         = 'CodigoAlternativo';

const

ERROR_BALANCETE_MENSAL_0001 = 'BALANC0001';
ERROR_BALANCETE_MENSAL_0002 = 'BALANC0002'; //Empresa Inexistente
ERROR_BALANCETE_MENSAL_0003 = 'BALANC0003';
ERROR_BALANCETE_MENSAL_0004 = 'BALANC0004';
ERROR_BALANCETE_MENSAL_0005 = 'BALANC0005';
ERROR_BALANCETE_MENSAL_0006 = 'BALANC0006';
ERROR_BALANCETE_MENSAL_0007 = 'BALANC0007';
ERROR_BALANCETE_MENSAL_0008 = 'BALANC0008';

I've tried that so far:

Match match = Regex.Match(delphiFileInText, @"const.+=\s*'(?<property>[\d\w]+)'", RegexOptions.IgnoreCase | RegexOptions.Singleline);

But all I get is that last match (BALANC0008);

I hope I can be clear. Thanks for help

like image 999
Warvimo Avatar asked Jun 26 '26 01:06

Warvimo


2 Answers

Simply replacing your expression with

'(?<property>[\d\w]+)'

will get all of them.

like image 194
Kit Avatar answered Jun 28 '26 15:06

Kit


I suggest the following Regular expression:

'(?<property>(?:\\'|[^'])*)'

Which will capture all of the single quote delimited strings in the input. If you want to capture the constants as well, I'd recommend the following regular expression:

(?<const>\w+)\s*=\s*'(?<property>(?:\\'|[^'])*)'
like image 26
FrankieTheKneeMan Avatar answered Jun 28 '26 15:06

FrankieTheKneeMan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!