7 people like it.

Using the Regex type provider

There are some nuances to using the regular expression type provider that are easy to miss. This snippet addresses them.

 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: 
/// First, you must open the FSharpx namespace, not the FSharpx.TypeProviders.Regex
/// namespace or even the FSharpx.TypeProviders namespace. (Or course, if you need
/// to reference the fully-qualified type name in order to disambiguate the Regex
/// type, be aware that it's simply "FSharpx.Regex")

open FSharpx

/// Next, define your Regex type. Remember that you're defining a type (duh!), 
/// *not* a let binding:

type NameRegex = Regex< @"(?<firstName>\p{L}+)\s+(?<lastName>\p{L}+)" >

/// Let's define a name to test
let name = "Giuseppe Verdi"

/// To test if the name matches the regular expression, use NameRegex without
/// parentheses to call the IsMatch ( string ) function:

NameRegex.IsMatch name 
|> printfn """Name.IsMatch "%s" = %b""" name

/// To parse the name, use NameRegex *with parentheses* to call the Match function. /// (You'll be instantiating a NameRegex object when you do so. Since it's not a
/// singleton, you may want to bind it to a symbol in order not to have to 
/// re-instantiate it if you use it again):

NameRegex().Match name
|> (fun m -> printfn """firstName: "%s", lastName: "%s" """ m.firstName.Value m.lastName.Value)
type NameRegex = obj


 Next, define your Regex type. Remember that you're defining a type (duh!),
 *not* a let binding:
val name : string


 Let's define a name to test
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
val m : obj

More information

Link:http://fssnip.net/ny
Posted:1 year ago
Author:musicologyman
Tags: type provider , type providers , regular expression , regular expressions , regex