-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegex.java
More file actions
71 lines (63 loc) · 1.35 KB
/
Copy pathRegex.java
File metadata and controls
71 lines (63 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
public class Regex
{
public static void main(String[] args)
{
System.out.println(isMatched("aa", "a"));
System.out.println(isMatched("aa", "aa"));
System.out.println(isMatched("aaa", "aa"));
System.out.println(isMatched("aa", "a*"));
System.out.println(isMatched("aa", ".*"));
System.out.println(isMatched("ab", ".*"));
System.out.println(isMatched("aab", "c*a*b*"));
System.out.println(isMatched("ab", "a*c"));
System.out.println(isMatched("aaa", "aaaa"));
System.out.println(isMatched("a", "..a*"));
System.out.println(isMatched("aaaaa", "a*"));
}
private static boolean isMatched(String s, String p)
{
// . matches all except \n
// *
// straight up match
if(s.matches(p))
return true;
char[] sc = s.toCharArray();
char[] c = p.toCharArray();
int i;
int j = 0;
// Go through regex
for(i = 0 ; i < c.length; i++)
{
// make sure we're within the bounds of the regex and string
if(j == sc.length && i != c.length)
return false;
// check dot
if(c[i] == '.')
{
if(sc[j] != '\n')
{
j++;
continue;
}
else
return false;
}
else if(c[i] == '*')
{
// skip *
}
else if(c[i] == sc[j])
{
// check norms
j++;
continue;
}
else
{
// if not then it's not a match
return false;
}
}
return i == sc.length;
}
}