F#: Regular expressions/active patterns
As I understand them active patterns are not that much different to normal functions but we can make use of them as part of a let or match statement which we can’t do with a normal function.
I wanted to create an active pattern that would be able to tell me if a Twitter status has a url in it and to return me that url. If there are no urls then it should tell me that as well.
This is therefore a partial active pattern as it does not necessarily describe something. Adapted from Chris Smith’s blog I therefore ended up with the following active pattern:
open System.Text.RegularExpressions let (|Match|_|) pattern input = let m = Regex.Match(input, pattern) in if m.Success then Some (List.tl [ for g in m.Groups -> g.Value ]) else None
This is a generic active pattern which will take in a string and a regular expression and return an Option containing the matches if there are some and none if there aren’t any.
The ‘_’ in the active pattern definition is the partial bit - we don’t necessarily have a match.
I quite liked what Chris did on line 4 of this statement whereby the results returned exclude the first item in the group of matches since this contains the entirety of the matched string rather than the individual matches.
I was then able to make use of the active pattern to check whether or not a Tweet contains a url:
let ContainsUrl value = match value with | Match "(http:\/\/\S+)" result -> Some(result.Head) | _ -> None
Active patterns seem pretty cool from my limited playing around with them and are something that I came across by chance when looking around for ways to use regular expressions in F#.