Created
July 17, 2011 20:12
-
-
Save palladin/1088007 to your computer and use it in GitHub Desktop.
Type based Regex Active Patterns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on http://blogs.msdn.com/b/chrsmith/archive/2008/02/22/regular-expressions-via-active-patterns.aspx | |
open System | |
open System.Text.RegularExpressions | |
let (|Match|_|) (pat:string) (inp:string) = | |
let m = Regex.Match(inp, pat) in | |
if m.Success | |
then Some (List.tail [ for g in m.Groups -> g.Value ]) | |
else None | |
let convert<'ToType> value = Convert.ChangeType(value, typeof<'ToType>) :?> 'ToType | |
let (|Match3|_|) (pat:string) (inp:string) : ('T1 * 'T2 * 'T3) option = | |
match (|Match|_|) pat inp with | |
| Some (fst :: snd :: trd :: []) -> | |
try | |
Some (convert<'T1> fst, convert<'T2> snd, convert<'T3> trd) | |
with _ -> failwith "Match3 succeeded, but with type conversion errors" | |
| Some [] -> failwith "Match3 succeeded, but no groups found. Use '(.*)' to capture groups" | |
| Some _ -> failwith "Match3 succeeded, but did not find exactly three matches." | |
| None -> None | |
// Example | |
let (month, day, year) : (int * int * int) = | |
match DateTime.Now.ToString() with | |
| Match3 "(\d*)/(\d*)/(\d*).*" (a,b,c) -> (a,b,c) | |
| _ -> failwith "Match Not Found." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment