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
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.