Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Recursive regex matcher with Leetcode-style semantics:
. matches any single character
<c>* matches zero or more of <c>
let rec is_match s i p j =
if j = String.length p then i = String.length s
else
let first = i < String.length s
&& (p.[j] = '.' || p.[j] = s.[i])
in
if j + 1 < String.length p && p.[j+1] = '*' then
is_match s i p (j + 2) (* skip * group *)
|| (first && is_match s (i + 1) p j) (* consume one *)
else
first && is_match s (i + 1) p (j + 1)
Patterns vs texts:
.a.b | aabb axb "" abcd abc aaabbbc x -> 1 match
a.*b | aabb axb "" abcd abc aaabbbc x -> 2 matches
x* | aabb axb "" abcd abc aaabbbc x -> 2 matches
a*b*c | aabb axb "" abcd abc aaabbbc x -> 2 matches
total = 7
Complements wildcard_match.ml which uses LIKE-style * / ?.
182 baseline programs total.
28 lines
674 B
OCaml
28 lines
674 B
OCaml
let rec is_match s i p j =
|
|
if j = String.length p then i = String.length s
|
|
else
|
|
let first =
|
|
i < String.length s
|
|
&& (p.[j] = '.' || p.[j] = s.[i])
|
|
in
|
|
if j + 1 < String.length p && p.[j + 1] = '*' then
|
|
is_match s i p (j + 2)
|
|
|| (first && is_match s (i + 1) p j)
|
|
else
|
|
first && is_match s (i + 1) p (j + 1)
|
|
|
|
let count_match pats texts =
|
|
let count = ref 0 in
|
|
List.iter (fun p ->
|
|
List.iter (fun t ->
|
|
if is_match t 0 p 0 then count := !count + 1
|
|
) texts
|
|
) pats;
|
|
!count
|
|
|
|
;;
|
|
|
|
let pats = [".a.b"; "a.*b"; "x*"; "a*b*c"] in
|
|
let texts = ["aabb"; "axb"; ""; "abcd"; "abc"; "aaabbbc"; "x"] in
|
|
count_match pats texts
|