How to check if a text contains two dates?

Hello,

I would like to check if a text contains two consecutive dates. For instance: “This text 01/03/2018 02/03/2018 is an example”.

Thanks a lot!

1 Like

you can use regex \d{1,2}/\d{1,2}/\d{4}

After you extract the dates out, as suggested, using either the Matches activity or vb net Regex.Matches(), you can use .ParseExact to convert them to datetime types and use .AddDays() on one to compare that they are consecutive.

Sorry if I make a mistake on the syntax here, but should look something like this:

Matches stored in enumerable variable I'll call dates
condition: DateTime.ParseExact(dates(0).ToString, "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentCulture).AddDays(1) = DateTime.ParseExact(dates(1).ToString, "dd/mm/yyyy", System.Globalization.CultureInfo.CurrentCulture)

So, compare first date +1 day with second date to check if they are equal

EDITED: mm to MM in “dd/MM/yyyy”… “mm” might mean minutes. “MM” means month

Regards.

3 Likes

Sorry, I didn’t explain well.
What I meant by “consecutive” was that they are located in the string one after the other one, not one day after another day.

This is an example: 02/03/2018 07/07/2019
This is not an example: 04/01/2018 is not an example 12/12/2017

Regards

You can use the isMatch activity for each date to look for a pattern like

(0?[1-9]|[12][0-9]|3[01])[- \/.](0?[1-9]|1[012])[- \/.](19|20)?\d\d[ ](0?[1-9]|[12][0-9]|3[01])[- \/.](0?[1-9]|1[012])[- \/.](19|20)?\d\d

or for each date pattern like

(0?[1-9]|[12][0-9]|3[01])[- \/.](0?[1-9]|1[012])[- \/.](19|20)?\d\d

Do not forget to check if also it is a valid date with .net DateTime

1 Like

Thank you very much.

I do not know how to use it.

Will it allow me to check if in a string there are two dates follow one before the other one?

This could be an example: “The text 04/10/2018 13/02/1922 is an example”.
This is not an example: “The text 04/10/2018 is not an example because they are not together 13/02/1922”.

Sorry for explaining so bad.

Thank you all very much.
@claytonm

im out of town. but i can answer any questions in a days :slight_smile:

Thanks a lot !

Hi @pal1910

You basically just need a pattern that will match with 2 dates next together. Then, you can use that pattern in the IsMatch activity or use Regex.Match().Success in a condition somewhere.

A pattern that might work is this:

"[0-9]{1,2}/[0-9]{1,2}/[0-9]{2,4}\s[0-9]{1,2}/[0-9]{1,2}/[0-9]{2,4}"

I basically just created a pattern for 1 date to check for 0-9 1 to 2 digits, a slash, and repeat… with a space (\s) in the middle.

To implement this in a condition you can use:
Regex.Match(text, pattern).Success

Or you can also place the pattern in the IsMatch activity and return a Boolean variable to use that in the condition.

To test this I used the Regex.Match().Success.ToString or Regex.Match().Value in a message box.

Regards.

1 Like

Thank you very much, It works.

How would you get the second date?

Thanks a lot!

Hi @pal1910

There are a few ways.

  1. you can use the same pattern (which should extract both dates), and knowing that it’s the second date separated by a space, you can use a .split()
    Regex.Match(text, pattern).Value.Split(" "c)(1)

  2. or you can adjust the pattern with a look behind “<=?”
    "(<=?([0-9]{1,2}/[0-9]{1,2}/[0-9]{2,4}\s))[0-9]{1,2}/[0-9]{1,2}/[0-9]{2,4}"
    Regex.Match(text, pattern).Value

Hope that answers it.

Thanks.

1 Like

Thank you very much @claytonM

One more question please. What if I do not know the number of blank spaces within the words?

“[0-9]{1,2}/[0-9]{1,2}/[0-9]{2,4}\s[0-9]{1,2}/[0-9]{1,2}/[0-9]{2,4}” wouldn’t work for “01/04/2018 02/04/2018” or for other sequence that has more than 1 blank space.

Thanks a lot again.

Hi @pal1910

You can add a “+” next to “\s” so it like can be 1 or more spaces.
\s+

If I’m wrong you can find some additional info on how to use Regex searching online.

Regards.

1 Like

Thanks a lot. It works perfectly. I will look for additional info on the forum. Thanks again.