Skip to content

Instantly share code, notes, and snippets.

@nagat01
Created June 5, 2011 00:05
Show Gist options
  • Save nagat01/1008514 to your computer and use it in GitHub Desktop.
Save nagat01/1008514 to your computer and use it in GitHub Desktop.
F# : Regex.IsMatch example
open System
open System.Text.RegularExpressions
// http://msdn.microsoft.com/en-us/library/ms228595(v=vs.80).aspx#Y300
let regexIsMatchSample lines pattern =
// for line in
List.iteri(fun i line ->
if Regex.IsMatch(line,pattern,RegexOptions.IgnoreCase) then
printfn"%3d : %s" i line) lines
regexIsMatchSample
["Hello, how are you?";
"How was this weekend?";
"Good evening!"]
"how"
let phones =
["123-456-7890";
"444-234-22450";
"690-203-6578";
"146-893-232";
"146-839-2322";
"4007-295-1111";
"407-295-1111";
"407-2-5555"]
for phone in phones do
printfn "%s - %s" phone
(if Regex.IsMatch(phone,@"\d{3}-\d{3}-\d{4}$") then "valid" else "invalid")
(*
0 : Hello, how are you?
1 : How was this weekend?
123-456-7890 - valid
444-234-22450 - invalid
690-203-6578 - valid
146-893-232 - invalid
146-839-2322 - valid
4007-295-1111 - valid
407-295-1111 - valid
407-2-5555 - invalid
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment