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)
namespace FSharpx
type NameRegex = Regex<...>

Full name: Script.NameRegex


 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")
 Next, define your Regex type. Remember that you're defining a type (duh!),
 *not* a let binding:
type Regex =
  inherit Regex
  member Options : RegexOptions
  member RightToLeft : bool
  val capnames : Hashtable
  val caps : Hashtable
  val capsize : int
  val capslist : string []
  val factory : RegexRunnerFactory
  val pattern : string
  val roptions : RegexOptions
  ...

Full name: FSharpx.Regex
val name : string

Full name: Script.name


 Let's define a name to test
Regex<...>.IsMatch(input: string) : bool


Indicates whether the regular expression finds a match in the specified input string

System.Text.RegularExpressions.Regex.IsMatch(input: string, pattern: string) : bool
System.Text.RegularExpressions.Regex.IsMatch(input: string, pattern: string, options: System.Text.RegularExpressions.RegexOptions) : bool
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val m : Regex<...>.MatchType
property Regex<...>.MatchType.firstName: System.Text.RegularExpressions.Group


Gets the "firstName" group from this match
property System.Text.RegularExpressions.Capture.Value: string
property Regex<...>.MatchType.lastName: System.Text.RegularExpressions.Group


Gets the "lastName" group from this match
Next Version Raw view Test code New version

More information

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