Question

I've tried figuring out how to make regex match something specific follow by a date and a time. I cannot for the life of me figure it out!

I want to match the following sentence, where the date and time of course may be random:

Den 25/01/2013 kl. 14.03 skrev

So it should match like this: Den dd/mm/yyyy kl. hh.mm skrev

Note that time is in 24-hour format.

Can anyone help here? I can easily find an example that matches a date or time, but I don't know how to combine it with this specific sentence :(

Thanks in advance

Was it helpful?

Solution

Use it just by combining them as:

Den (0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/(0{3}[1-9]|((?!0{3}\d)\d{4})) kl\. ([01][0-9]|[2[0-3])\.([0-5][0-9]) skrev

Note : Date not validated properly. Will match 30/02/2000

Den matches Den as such.

(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/\d{3}[1-9] matches date. 0{3}[1-9]|((?!0{3}\d)\d{4}) avoids 0000 as year.

kl\. matches kl. The \ before the . is to escape . which is a special character in regex

([01][0-9]|[2[0-3])\.([0-5][0-9]) matches time from 00.00 to 23.59

skrev matches skrev as such.

The following validates date a bit more well

Den ((0[1-9]|[12][0-9]|3[01])/(?=(0[13578]|1[02]))(0[13578]|1[02])|(0[1-9]|[12][0-9]|30)/(?=(0[469]|11))(0[469]|11)|(0[1-9]|[12][0-9])/(?=(02))(02))/(0{3}[1-9]|((?!0{3}\d)\d{4})) kl\. ([01][0-9]|[2[0-3])\.([0-5][0-9]) skrev

Still matches 29/02/1999 - No validation for leap year or not

To match single digit days and months also, replace the date part with the following:

(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[0-2])/(0{3}[1-9]|((?!0{3}\d)\d{4}))

The ? makes the preceding part optional i.e. the 0 becomes optional.

OTHER TIPS

`Den ([0-3]\d)/([0-1]\d)/(\d{4}) kl\. ([0-2]\d)\.([0-5]\d) skrev`

to catch the values in order to facilitate validation.

maybe not the smartest solution, but this expression should fit your request:

Den\s[0-3][0-9]/[0-1][[0-9]/[0-9][0-9][0-9][0-9]\skl.\s[0-2][0-9].[0-6][0-9]\sskrev

Turns out all I actually needed was:

(Den ../../.... kl. ..... skrev)

Since . just matches random characters, and because this sentence is auto-generated by an e-mail client, there is no need to actually validate if it's a date, but merely look for this pattern and discard everything after. Nobody would ever write that so specifically in the middle of regular text.

In case anybody is wondering, this is for SpiceWorks reply header filtering.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top