Home
Insert
Update snippet 'Using the Regex type provider'
Title
Description
There are some nuances to using the regular expression type provider that are easy to miss. This snippet addresses them.
Source code
/// 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)
Tags
type provider
type providers
regular expression
regular expressions
regex
type provider
type providers
regular expression
regular expressions
regex
Author
Link
Reference NuGet packages
If your snippet has external dependencies, enter the names of NuGet packages to reference, separated by a comma (
#r
directives are not required).
Update