问题
On the shared-host where my website is, I have an e-mail forwarder that sends messages to my phone as a text message. Recently, however, it's been getting a bit out of hand.
I am hoping that, using the "User Level Filtering" in cPanel, I can setup a RegEx-based filter to only forward e-mails to my phone between the hours of 07:00:00 and 22:00:00.
This filter would match the following e-mail header, which typically occurs 2-3 lines into the message source:
Received: by 99.99.99.99 with SMTP id XyXyXyXyXyXyXy;
Tue, 15 May 2012 01:22:33
... and this is how I am hoping it'd work:
Received: by (ANY IP) with (ANYTHING) id (ANYTHING);
(Sun|Mon|Tue|Wed|Thu|Fri|Sat), (ANY DATE/MONTH/YEAR) (RegEx magic to match time?)
When the match succeeds (e.g. If the time is between 07:00:00 and 22:00:00) the message would then be sent to my phone, otherwise it would do nothing. (This has nothing to do with the RegEx itself, of course.)
I'm quite a Regular Expressions n00b, but have devised the following (non-working) RegEx, and have no idea how close or far away I am from it working:
\bReceived: by (.*) with [a-Z] id (.*)(Sun|Mon|Tue|Weds|Thu|Fri|Sat),(.*)[0-2][2-7]:[0-2][0-9]:[0-2][0-9]\bsi
Any thoughts, suggestions or creative solutions? I have been told by my hosting service that RegEx in cPanel needs to be in PERL format.
回答1:
Amended version of Eugene's regex;
/\bReceived: by .*?;\s*(?:Sun|Mon|Tue|Weds|Thu|Fri|Sat),\s*\d+\s+\w+\s+\d+\s+(?:0[789]|1\d|2[01]):\d\d:\d\d\b/
This version uses non-capturing grouping, and restricts the acceptable time to 07:00:00 to 21:59:59. It assumes the time format is valid - in theory it would pass a time of 10:99:99, but I'm assuming you won't see those.
回答2:
Fixed for you to work using http://gskinner.com/RegExr/, still largely suboptimal. Tell if you want to go into this further.
\bReceived: by (.*) with \w+ id (.*);\s*(Sun|Mon|Tue|Weds|Thu|Fri|Sat),\s*(\d+\s+\w+\s+\d+)\s+[0-2][0-9]:[0-5][0-9]:[0-5][0-9]\b
来源:https://stackoverflow.com/questions/10689862/regex-to-match-time-in-received-by-e-mail-header