D-Programming-Language/phobos — pull request #1241 by someboddy (2013-04-04 17:06:46+00:00)
Title: Add functional style regex pattern-matching

Based on Scala's pattern-matching with regular expressions syntax, I created the function `std.regex.switchRegex`. It's main use-case is for reading textual input fields that can be in multiple formats:

```D
enum WeekDay
{
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
}

struct Schedule
{
WeekDay day;
int hour;
}

assert(equal(
[
Schedule(WeekDay.Sunday, 1),
Schedule(WeekDay.Monday, 14),
Schedule(WeekDay.Tuesday, 3),
Schedule(WeekDay.Wednesday, 4),
Schedule(WeekDay.Thursday, 17),
Schedule(WeekDay.Friday, 6),
],
[
"Sunday 1AM",
"Monday 2PM",
"Tuesday 3",
"4AM Wednesday",
"5PM Thursday",
"6 Friday",
].map!(schedule => schedule.switchRegex(
`(\w+) (\d+)AM`, (WeekDay day, int hour) => Schedule(day, hour % 12),
`(\w+) (\d+)PM`, (WeekDay day, int hour) => Schedule(day, hour % 12 + 12),
`(\w+) (\d+)`, (WeekDay day, int hour) => Schedule(day, hour),
`(\d+)AM (\w+)`, (int hour, WeekDay day) => Schedule(day, hour % 12),
`(\d+)PM (\w+)`, (int hour, WeekDay day) => Schedule(day, hour % 12 + 12),
`(\d+) (\w+)`, (int hour, WeekDay day) => Schedule(day, hour),
))()));
```