Skip to content

Commit 6ca64cc

Browse files
author
openset
committed
Add: Regular Expression Matching
1 parent c74eae8 commit 6ca64cc

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
11
package regular_expression_matching
2+
3+
import "regexp"
4+
5+
func isMatch(s string, p string) bool {
6+
reg := regexp.MustCompile(p)
7+
res := reg.FindAllString(s, 1)
8+
return len(res) == 1 && res[0] == s
9+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
11
package regular_expression_matching
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
s string
7+
p string
8+
expected bool
9+
}
10+
11+
func TestIsMatch(t *testing.T) {
12+
tests := [...]caseType{
13+
{
14+
s: "aa",
15+
p: "a",
16+
expected: false,
17+
},
18+
{
19+
s: "aa",
20+
p: "a*",
21+
expected: true,
22+
},
23+
{
24+
s: "ab",
25+
p: ".*",
26+
expected: true,
27+
},
28+
{
29+
s: "aab",
30+
p: "c*a*b",
31+
expected: true,
32+
},
33+
{
34+
s: "mississippi",
35+
p: "mis*is*p*.",
36+
expected: false,
37+
},
38+
{
39+
s: "",
40+
p: ".",
41+
expected: false,
42+
},
43+
}
44+
for _, tc := range tests {
45+
output := isMatch(tc.s, tc.p)
46+
if output != tc.expected {
47+
t.Fatalf("input: %v %v, output: %v, expected: %v", tc.s, tc.p, output, tc.expected)
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)