Skip to content

Instantly share code, notes, and snippets.

@htsign
Last active May 20, 2017 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save htsign/63e409594371364725bee01d4611b006 to your computer and use it in GitHub Desktop.
Save htsign/63e409594371364725bee01d4611b006 to your computer and use it in GitHub Desktop.
module RegExp
open System.Text.RegularExpressions
type NamedValue = Map<string, string>
let private noneOrList = function [] -> None | xs -> Some xs
let private getValues (m : Match) = List.tail [for g in m.Groups -> g.Value]
let private getNamedValues (ns : string []) (gs : GroupCollection) = List.tail [for n in ns -> n, gs.[n].Value] |> Map.ofList
let (|Match|_|) pattern input : string list option =
let m = Regex.Match(input, pattern)
getValues m |> noneOrList
let (|Matches|_|) pattern input : string list list option =
let ms = Regex.Matches(input, pattern)
[for m in ms -> getValues m] |> noneOrList
let (|NamedMatch|_|) pattern input : NamedValue option =
let re = Regex(pattern)
let names = re.GetGroupNames()
let gs = re.Match(input).Groups
let map = getNamedValues names gs
if Map.isEmpty map then None else Some map
let (|NamedMatches|_|) pattern input : NamedValue list option =
let re = Regex(pattern)
let names = re.GetGroupNames()
let ms = re.Matches(input)
let gss = [for m in ms -> m.Groups]
gss |> List.map (getNamedValues names) |> noneOrList
module RegExp
type NamedValue = Map<string,string>
val private noneOrList : _arg1:'a list -> 'a list option
val private getValues : m:System.Text.RegularExpressions.Match -> string list
val private getNamedValues :
ns:string [] ->
gs:System.Text.RegularExpressions.GroupCollection -> Map<string,string>
val ( |Match|_| ) : pattern:string -> input:string -> string list option
val ( |Matches|_| ) : pattern:string -> input:string -> string list list option
val ( |NamedMatch|_| ) : pattern:string -> input:string -> NamedValue option
val ( |NamedMatches|_| ) :
pattern:string -> input:string -> NamedValue list option
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment