#light
let regex s = new System.Text.RegularExpressions.Regex(s,System.Text.RegularExpressions.RegexOptions.Compiled)

let stringorregex (reg : obj) =
   match reg with
   | :? System.Text.RegularExpressions.Regex -> reg :?> System.Text.RegularExpressions.Regex
   | :? string -> System.Text.RegularExpressions.Regex (reg :?> string)
   | _ -> failwith "regular expression must be of type <regex> or <string>."

let (=~) reg str = (stringorregex reg).IsMatch(str)
let (^~) reg str = not ((stringorregex reg).IsMatch(str))

let (=~~) (reg : obj) str =
   let m = (stringorregex reg).Match(str)
   [0..m.Groups.Count - 1] |> List.map (fun x -> (m.Groups.Item(x)).Value)

let (=~~~) (reg : obj) str =
   let groups (m : System.Text.RegularExpressions.Match) = [0..m.Groups.Count - 1] |> List.map (fun x -> (m.Groups.Item(x)).Value)
   let m = ref ((stringorregex reg).Match(str))
   seq {
      while (!m).Success do
         yield groups (!m)
         m := (!m).NextMatch()
   }
 



Example usage


regex "d[a-z]g" =~ "the dog is brown" |> printf "%b\n"

prints: true


"d[a-z]g" ^~ "the dog is brown" |> printf "%b\n"

prints: false


let [_; the; pen; works; correctly] = "(\w+) (\w+) (\w+) (\w+)" =~~ "the pen works correctly"
printf "%s,%s,%s,%s\n" the pen works correctly

prints: the,pen,works,corectly


for i in "(\w+) (\w+)" =~~~ "james 12 charles 78 helmut 43" do
   printf "[%s,%s] " i.[1] i.[2]

prints: [james,12] [charles,78] [helmut,43]


".o" =~~~ "do you remember willo the wisp?"
   |> Seq.map (fun (x :: []) -> x)
   |> Seq.iter (printf "[%s] ")

prints: [do] [yo] [lo]


let split = (regex " ").Split("i fancy he is a bedlamite")
for word in split do printf "%s, " word

prints: i, fancy, he, is, a, bedlamite,


[1..100]
   |> List.map string
   |> List.filter ((=~) "1.+")
   |> List.iter (printf "%s,")

prints: 10,11,12,13,14,15,16,17,18,19,100,