open System.Text.RegularExpressions // the definition of the active pattern let (|Regex|_|) regexPattern input = // create and attempt match regular expression let regex = new Regex(regexPattern) let regexMatch = regex.Match(input) // return either Some or None if regexMatch.Success then Some regexMatch.Value else None // function to print the results by pattern // matching over different instances of the // active pattern let printInputWithType input = match input with | Regex "$true|false^" s -> printfn "Boolean: %s" s | Regex @"$-?\d+^" s -> printfn "Integer: %s" s | Regex "$-?\d+\.\d*^" s -> printfn "Floating point: %s" s | _ -> printfn "String: %s" input // print the results printInputWithType "true" printInputWithType "12" printInputWithType "-12.1"