Problem reading the month with Regex


First of all hello everyone. My name is Nacho and I have a problem with a process in my company. My robot downloads a series of invoices and saves them in a folder corresponding to the month in which the invoice was issued.

Its operation is as follows:

1 - Download the invoice.
2 - Use a Regex to read the invoice date.
3 - Depending on the month read on the invoice, save it in the folder for the current month (for example, in the folder 2-2022 if it is from February, or in the folder 1-2022 if it is from January).

The thing is that I can’t get it to work well for me, since it always ends up saving them in the previous month instead of in the current month.

The assignment I use is the following:

MesFact = Convert.ToDouble(system.Text.RegularExpressions.Regex.Match(pdf, “\d{0,2}[/-.]\d{0,2}[/-.]\d{2,4}”, RegexOptions.Multiline).Value.ToString.Substring(3,2))

I convert the variable “MesFact” to Double after having searched the document for something that matches the date of the month and I keep (supposedly) the value of the date of the month with the “Substring (3,2)”.

It is worth mentioning that I am from Spain and therefore the dates here follow the format dd/mm/yyyy, or dd-mm-yyyy instead of the American format which would be mm/dd/yyyy.

After making that assignment I do the following conditional:


MesFactura = Convert.ToDouble(DateTime.Today.AddMonths(0).ToString(new system.Globalization.CultureInfo(“es-ES”)).ToString.Substring(3,2))


Move the file to the current month’s folder.


Move the file to the previous month’s folder.

I would like to know what I am doing wrong, if the regex is correct, if the conditional is correctly implemented, if I am saving the value of the month correctly or on the contrary it takes another value, because it is as if the conditional always evaluates to false.

Thanks in advance!

All the best


How about the following?

m = System.Text.RegularExpressions.Regex.Match(pdf, "(?<DAY>\d{1,2})[/\-.](?<MONTH>\d{1,2})[/\-.](?<YEAR>\d{2,4})", RegexOptions.Multiline)

MesFact = Int32.Parse(m.Groups("MONTH").Value)


Thank you so much!!

Now I’m going to try it but till monday I won’t be sure if it works.