title: Scala, C# equivalent of F# active patterns
body: <p>F# has a feature enabling users to extend pattern matching:</p>
  
  <pre><code>let (|Even|Odd|) n = if n % 2 = 0 then Even else Odd
  match 3 with | Odd -&gt; printfn "odd"
               | Even -&gt; printfn "even"
  </code></pre>
  
  <p>or:</p>
  
  <pre><code>(** Parsing date from any of the formats: MM/DD/YY, MM/DD/YYYY, YYYY-MM-DD *)
  let parseDate = function
  | ParseRegex "(\d{1,2})/(\d{1,2})/(\d{1,2})$" [Integer m; Integer d; Integer y]
     -&gt; new DateTime(y + 2000, m, d)
  | ParseRegex "(\d{1,2})/(\d{1,2})/(\d{3,4})" [Integer m; Integer d; Integer y]
     -&gt; new DateTime(y, m, d)
  | ParseRegex "(\d{1,4})-(\d{1,2})-(\d{1,2})" [Integer y; Integer m; Integer d]
     -&gt; new DateTime(y, m, d)
  </code></pre>
  
  <p>The magic happens in the ParseRegex pattern which is defined as such:</p>
  
  <pre><code>(** Definition of the MatchRegex active pattern *)
  let (|ParseRegex|_|) rgx s = match Regex(rgx).Match(s) with
                           | m when not m.Success -&gt; None
                           | m -&gt; Some(List.tail [for x in m.Groups-&gt;x.Value])
  </code></pre>
  
  <p>The ParseRegex can now be used everywhere and the whole point of it is that parsing out parts of strings can be done in a very <em>concise</em> way.</p>
  
  <p>Does Scala have a similar feature?</p>
  
  <p>In C# this level of conciseness seems hard to achieve but maybe some clever trick using implicit conversion or so could help?</p>