Created
May 17, 2018 12:18
-
-
Save jtpaasch/87b591f7be23aae07e137560920a4478 to your computer and use it in GitHub Desktop.
A simple string matcher (OCaml). Tries a literal match first, then a regex match.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let has_match tests = | |
| let matches = List.filter (fun s -> s = true) tests in | |
| List.length matches > 0 | |
| let is_exact_match str_1 str_2 = str_1 = str_2 | |
| let is_regex_match str_1 str_2 = | |
| let r = Str.regexp str_1 in | |
| Str.string_match r str_2 0 | |
| let compare str_1 str_2 = | |
| let is_exact = is_exact_match str_1 str_2 in | |
| Printf.printf "Is exact match: %b\n%!" is_exact; | |
| let is_regex = is_regex_match str_1 str_2 in | |
| Printf.printf "Is regexp match: %b\n%!" is_regex; | |
| has_match [is_exact; is_regex] | |
| let main () = | |
| let str_1 = Sys.argv.(1) and str_2 = Sys.argv.(2) in | |
| Printf.printf "STR 1:\n%s\n\n%!" str_1; | |
| Printf.printf "STR 2:\n%s\n\n%!" str_2; | |
| let matches = compare str_1 str_2 in | |
| Printf.printf "\nMATCHED: %b\n%!" matches | |
| let () = main () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment