1

The Fsharpx.Extras NuGet package exposes an active pattern for regular expression matching, qualified as Fsharpx.Text.Regex.Match.

The first parameter is a RegexOptions value from the BCL.

Rather than having to write:

let someFunc =
    | Match RegexOptions.None "...pattern 1..." matches -> ...
    | Match RegexOptions.None "...pattern 2..." matches -> ...
    | Match RegexOptions.None "...pattern 3..." matches -> ...
    ...

I was hoping it would be possible to instead have (using a revised Match' active pattern):

let someFunc =
    | Match' "...pattern 1..." matches -> ...
    | Match' "...pattern 2..." matches -> ...
    | Match' "...pattern 3..." matches -> ...
    ...

One possible definition of Match' I came up with was:

let (|Match'|_|) pattern =
    function
    | Match RegexOptions.None pattern matches -> Some matches
    | _ -> None

...which works fine. However, I couldn't help wonder if there was another approach similar to a partially applied function such as:

let (|Match'|_|) =
    Match RegexOptions.None

Frustratingly, this complains about Type has no accessible object constructors..

Is something similar to the latter (alebit failing) approach possible?

RMills330
  • 319
  • 1
  • 12

1 Answers1

5

Open the Regex module, then change your last example to

let (|Match|_|) = (|Match|_|) RegexOptions.None

In fact, if you look at the source code, you'll see an example of this in the Compiled module.

https://github.com/fsprojects/FSharpx.Extras/blob/master/src/FSharpx.Extras/Regex.fs

Jim Foye
  • 1,918
  • 1
  • 13
  • 15
  • Thank you... Also great spot regarding an example of this in the source code... It didn't even occur to me to check there. Much appreciated! – RMills330 Dec 31 '21 at 23:30