How to validate Date with Regex?

Asked

Viewed 838 times

2

I am trying to capture a date inside a string using regex, but when I try to recover the part of the string returns me the following error:

analyzing " TARIFF:([ d/]+)" - Unrecognised exhaust sequence T.

 match = Regex.Match(sbDados2.ToString(), @"\TARIFA:([\d\/]+)");

if (match.Success)
{
    objDocInterfDoc.inicioVigencia = Validacao.ConverterCampoDateTime(new Entrada
    {
        Conteudo = Funcoes.RemoverEspacos(match.Groups[1].Value)
    }).Data;
}

My string:

{BUDGET NUMBER~11727497~INSURANCE OFFER~ISSUE N. COMPANY PROPOSAL~PORT INSURANCE CIA GENERAL INSURANCE~06/06/2016~69 - 63703827~AVENIDA RIO BRANCO, 1489 - CAMPOS ELÍSEOS~RENOVA PORTO INSURANCE POLICY~TYPE OF INSURANCE:68 - 759817~PORTO RENOVATION~SÃO PAULO - CEP 01205-905~SITE: WWW.PORTOSEGURO.COM.BR~DIA 24H DURATION~TARIFA:08/06/2016~MAIO/2016~REGISTRATION CODE WITH SUSEP 05886~ATÉ AS 24H DO DIA~IMPRESSÃO:~08/06/2017~08/06/2016 - 09:26:12~C.N.P.J. 61.198.164/0001-60~EP SUSN.: 15414.000573/2006-42}

I want recovers part RATE:08/06/2016.

When I try to do through the regex101 , it correctly validates that part.

  • Try using this regular expression.

  • What was missing in the question to deny?

  • I do not know, for me there is no problem in the question.

1 answer

6


Just remove this character (the backslash - \), if I understand what you need, your regex doesn’t need it to work.

The example below works right

var sbDados2 = "TARIFA:25/04/2016";     
var match = Regex.Match(sbDados2.ToString(), @"TARIFA:([\d\/]+)");

Check the dotNetFiddle

  • 1

    The reason for this is that the \t is an anchor that means TAB, and usually some anchors have their opposite : \w = \W, \d = \D which means the same catch only denied. however the \t does not have it, so must be generating the error, the compiler must be looking for the denied anchor \T, that doesn’t exist.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.