Regular Expression in F# is used with Active pattern, in most cases.
open System.Text.RegularExpressions
let (|Matcher|_|) pattern input =
let matcher = Regex.Match(input, pattern)
if matcher.Success then Some (List.tail [ for group in matcher.Groups -> group.Value ])
else None
let matchHttpUrl target =
match target with
| Matcher @"(https?:\/\/\S+)" result -> Some(result.Head)
| _ -> None
> matchHttpUrl "http://www.google.com";;
val it : string option = Some "http://www.google.com"
> matchHttpUrl "https://www.google.com";;
val it : string option = Some "https://www.google.com"
> matchHttpUrl "ftp://www.google.com";;
val it : string option = None
No comments:
Post a Comment