34 people like it.
Like the snippet!
Using Active patterns
Show's how to create a parametrized active pattern that will match based on a regular expression.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
|
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"
|
namespace System
namespace System.Text
namespace System.Text.RegularExpressions
Multiple items
type Regex =
new : pattern:string -> Regex + 1 overload
member GetGroupNames : unit -> string[]
member GetGroupNumbers : unit -> int[]
member GroupNameFromNumber : i:int -> string
member GroupNumberFromName : name:string -> int
member IsMatch : input:string -> bool + 1 overload
member Match : input:string -> Match + 2 overloads
member Matches : input:string -> MatchCollection + 1 overload
member Options : RegexOptions
member Replace : input:string * replacement:string -> string + 5 overloads
...
Full name: System.Text.RegularExpressions.Regex
--------------------
Regex(pattern: string) : unit
Regex(pattern: string, options: RegexOptions) : unit
val regexPattern : string
val input : string
val regex : Regex
val regexMatch : Match
Regex.Match(input: string) : Match
Regex.Match(input: string, startat: int) : Match
Regex.Match(input: string, beginning: int, length: int) : Match
property Group.Success: bool
union case Option.Some: Value: 'T -> Option<'T>
property Capture.Value: string
union case Option.None: Option<'T>
val printInputWithType : input:string -> unit
Full name: Script.printInputWithType
Multiple items
active recognizer Regex: string -> string -> string option
Full name: Script.( |Regex|_| )
--------------------
type Regex =
new : pattern:string -> Regex + 1 overload
member GetGroupNames : unit -> string[]
member GetGroupNumbers : unit -> int[]
member GroupNameFromNumber : i:int -> string
member GroupNumberFromName : name:string -> int
member IsMatch : input:string -> bool + 1 overload
member Match : input:string -> Match + 2 overloads
member Matches : input:string -> MatchCollection + 1 overload
member Options : RegexOptions
member Replace : input:string * replacement:string -> string + 5 overloads
...
Full name: System.Text.RegularExpressions.Regex
--------------------
Regex(pattern: string) : unit
Regex(pattern: string, options: RegexOptions) : unit
val s : string
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
More information