tl;dr: Make invalid regular expression strings and attempts to access non-existent capture groups a compile-time error, thanks to the Regex type provider.
Standard .NET regex
Say we want to parse out some information from basic Liquid tags, like this:
F# type provider version
First up we need to add the RegexProvider to our project via nuget:
PM> Install-Package RegexProvider.
Now we can rewrite our previous implementation like this:
This will compile equivalently to our previous implementation2, but we’ve gained some nice static checks.
We can access the
contents capture groups of our match as properties. This isn’t a
method_missing-style dynamic lookup – if we rename the group in the regex to
(?<notTag>\w+) then we get a compile-time error:
error FS0039: The field, constructor or member 'tag' is not defined
Also neat, if we completely muck up our regex, the compiler will let us know:
error FS3033: The type provider ... reported an error: parsing "[asd" - Unterminated  set.
Tests would catch both these errors, but feedback doesn’t get much faster than “as we’re typing the code”, plus we get precise line numbers for the errors as well. It also reduces code noise, dealing directly with the capture group names rather than having to specify particular collection lookups.