1

I have a excel file that must have this name format, where xxx is a number and yymmdd is a date. Only xxx and yymmdd change, the rest is always the same.

CDFSDDRCxxxCurryymmdd.xls

What is a regex I can use to check if it is correct??

Rui Martins
  • 2,164
  • 7
  • 33
  • 52

3 Answers3

3

Try with following regex:

^CDFSDDRC\d{3}Curr\d{6}\.xls$
hsz
  • 148,279
  • 62
  • 259
  • 315
0

In C# I think you can try something like this :

"CDFSDDRC(?<xxx>[0-9]+)Curr(?<yymmdd>[0-9][0-9][0|1][0-9][0-3][0-9])\.xls"

But you will have to check matches.Groups["yymmdd"].value once more to check for special cases such as CDFSDDRC123Curr321539.xls that match but contains an incorrect date.

Eregrith
  • 4,263
  • 18
  • 39
0

I think the following should work.

CDFSDDRC\d[3]Curr(([0-9]{2}(0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|([0-9]{2}(0[469]|1[1])(0[1-9]|[12][0-9]|30))|([0-9]{2}(02)(0[1-9]|1[0-9]|2[0-8]))|((((04|08|[2468][048]|[13579][26]))|00)(02)29))\.xsl

I think that you have to escape the backslashes in the C# string.

"CDFSDDRC\\d[3]Curr(([0-9]{2}(0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|([0-9]{2}(0[469]|1[1])(0[1-9]|[12][0-9]|30))|([0-9]{2}(02)(0[1-9]|1[0-9]|2[0-8]))|((((04|08|[2468][048]|[13579][26]))|00)(02)29))\\.xsl"
Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53