Skip to content

Instantly share code, notes, and snippets.

@nagat01
Created June 5, 2011 04:32
Show Gist options
  • Save nagat01/1008649 to your computer and use it in GitHub Desktop.
Save nagat01/1008649 to your computer and use it in GitHub Desktop.
F# : Regex.Match, Regex.Matches
(*
http://msdn.microsoft.com/en-us/library/30wbz966(v=vs.71).aspx#Y470
Match : finds first occurrence
Match.Success : is match succeed?
Match.Groups : collection of captured groups
Matches : store every non-overlapping occurrences to Item
Matches.Item : indexed property of mathed string
*)
open System
open System.Text.RegularExpressions
let matchSample r m =
let r = new Regex(r)
let m1 = r.Match m
printfn "m.Groups = %A" m1.Groups
printfn "m.Captures = %A" m1.Captures
let ms = r.Matches m
printfn "ms.Count = %A" ms.Count
for i in 0..ms.Count-1 do
printfn "ms.Item.[%d] = %A" i <| ms.Item i
matchSample "a(bc)d(e(fg))h" "abcdefgh_abcdefgh"
(* Result :
m.Groups = seq [abcdefgh; bc; efg; fg]
m.Captures = seq [abcdefgh]
ms.Count = 2
ms.Item.[0] = abcdefgh
ms.Item.[1] = abcdefgh
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment