Q:

Getting to grips with regex in Perl

I'm trying to write a regular expression to match this line:
DD MONTH YEAR at HH:MM
as an example:
21 May 2009 at 19:09
So I have:
[0-30-9] for the day
[0-20-90-90-9] for the year
[0-90-9:0-90-9] for the time
I don't understand how to put these all together to form one single regex. I want to do
if($string =~ /myregex/) {
}
But can't form the entire thing. Also I don't know how to write a regex for the month, it has to match one of the 12 months of the year.
I am a Perl noob (this is my first day) and a regex noob, so help appreciated!

A:

Well, the parts you have aren't quite correct. Instead of [0-30-9] I think you mean [0-3][0-9], and similarly for the other numbers.
However, usually it suffices to be a little looser and just use \d which is equivalent to [0-9].
You string the parts together one after the other:
/\d\d (MONTH) \d\d\d\d at \d\d:\d\d/

Which can be written more succinctly as:
/\d\d (MONTH) \d{4} at \d\d:\d\d/

Or if you really need it to be more strict as in your formulation:
/[0-3]\d (MONTH) [0-2]\d{3} at \d\d:\d\d/

I've left the month bit for last, since it is the more complicated bit. Again you can be loose or strict.
Loosely:
/[0-3]\d [A-Za-z]+ [0-2]\d{3} at \d\d:\d\d/

For a strict match we can use an alternation, each alternative is separated by a '|' and the list of choices is enclosed in parenthesis (although beware, parenthesis also have another extra meaning; don't worry it won't interfere in this case):
/[0-3]\d (January|February|March|April|May|June|July|August|September|October|November|December) [0-2]\d{3} at \d\d:\d\d/

Finally, if the day is not 0-padded (meaning the 1st is just '1' rather than '01') then you need to make that optional:
/[0-3]?\d (January|February|March|April|May|June|July|August|September|October|November|December) [0-2]\d{3} at \d\d:\d\d/

Crib sheet

[] are used to create a character class, a set of matching characters
\d is a built-in character class equivalent to [0-9]
() are used to create a group, useful for delimiting an alternation (amongst other things)
| is used to create alternation, a list of alternative character sequences that should be matched
{n} is a modifier, saying exactly 'n' of the preceding character or character class should be matched
+ is a modifier, saying 1 or more of the preceding character or character class should be matched
? is a modifier, saying 0 or 1 of the preceding character or character class should be matched

