Question

Matching the right pattern

  • 1 September 2022
  • 4 replies
  • 43 views

Hey everyone,

 

I have a zap that listens for a Discord message then searches that message for a date and time. To search for the pattern of the date I’m using “Extract Pattern” on the Formatter using this pattern: 

\d/\d\d/\d\d\d\d|\d\d/\d/\d\d\d\d|\d/\d/\d\d\d\d|\d/\d/\d\d|\d/\d\d/\d\d|\d\d/\d/\d\d|\d\d/\d\d/\d\d|\d/\d\d/\d\d\d\d

Essentially this is just looking for all variations of typing the date. This works pretty well except when people put a 4 digit year. Even though I have the pattern in there, the zap just picks the first two digits out of the year and thinks it’s found the MM/DD/YY pattern instead. So now people putting the date 2022 are having their events post in ‘20. I’ve tried putting the 4 digit year pattern at the beginning and end of the “Pattern” field but this continues to happen either way.

 

How can I make sure the zap searches for the 4 digit year first and if it finds it, uses that instead of looking for another possible match?

 

Thanks!


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

4 replies

Userlevel 7
Badge +12

Hi @FWD!

I am by no means a regex expert, but I did some Googling and found this expression, which might help:

^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$

 

I think that the format of this will match for dates that are day/month/year, so if you’re working with US formatted dates, you’ll need to switch it around a bit. I found a way of testing it on this site. As an fyi, that’s not an official Zapier resources, just something a found with some Googling, so do check the formula for yourself before you use it. 

 

If anyone else has any ideas on how to edit @FWD’s query to account for years that may be 2 or 4 digits, it’d be great to hear them!

Hey Danvers,

 

This seems to capture some date formats but not all of them. I unfortunately don’t have the know how to fix this to include the ones I need which are:

 

MM/DD/YYYY, M/DD,YYYY, MM/D/YYYY, M/D/YYYY, MM/DD/YY, M/DD/YY, MM/D/YY, and M/D/YY

 

 

Just a quick update, I found a good expression: ^(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$

 

That matches all formats I need, however it doesn’t seem to extract the pattern out of a wall of text. Only if the only input is the date itself. For my purposes, I need the expression to find the date in a wall of text like this:

Date to be sent: 09/05/2022
Time to be sent: 10:00AM

 

Can someone please help me modify this to search in the full message and not only look for the full message to match the pattern? Thanks in advance!

Userlevel 7
Badge +9

Hey @FWD! I’ve checked with a few folks on our end but unfortunately, we’re not super familiar/well-versed in regex expressions. 😔 So while we can’t help from the Zapier side, I will leave this thread open incase there is another community member who might be able to help!

CCing @Troy Tessalone, @MohSwellam @Moshe770 and @andywingrave here incase any of y’all have ideas to share!