D-Programming-Language/phobos — pull request #1392 by someboddy (2013-07-05 19:25:58+00:00)
Title: fix issue 9895 - Add functional style regex pattern-matching(take 2)

(this is the same as [pull request 1241](https://github.com/D-Programming-Language/phobos/pull/1241). I had to close the old one after a failed rebasing messed it up)

Based on Scala's pattern-matching with regular expressions syntax, I created the function `std.regex.regexSwitch`(formerly `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!(regexSwitch!(
`(\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),
))()));
```