forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicRegexParser.java
59 lines (54 loc) · 2.57 KB
/
BasicRegexParser.java
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
package com.rampatra.strings;
/**
* @author rampatra
* @since 25/11/2018
*/
public class BasicRegexParser {
/**
* Implement a regular expression function isMatch that supports the '.' and '*' symbols.
* The function receives two strings - text and pattern - and should return true if the
* text matches the pattern as a regular expression. For simplicity, assume that the actual
* symbols '.' and '*' do not appear in the text string and are used as special symbols only
* in the pattern string.
*
* @param text
* @param pattern
* @return
*/
private static boolean isMatch(String text, String pattern) {
int textIndex = 0;
int patternIndex = 0;
while (textIndex < text.length()) {
if (patternIndex >= pattern.length()) return false;
if (pattern.charAt(patternIndex) == '.' || text.charAt(textIndex) == pattern.charAt(patternIndex)) {
// if the next character in the pattern is a '*' then forward the text pointer
if (patternIndex < pattern.length() - 1 && pattern.charAt(patternIndex + 1) == '*') {
while (textIndex < text.length() - 1 && text.charAt(textIndex + 1) == text.charAt(textIndex)) {
textIndex++;
}
patternIndex++; // for the '*' in the pattern
}
} else if (patternIndex < pattern.length() - 1 && pattern.charAt(patternIndex + 1) == '*') { // if the text
// and pattern are not equal at the index but the pattern has a '*' after then increment the pattern pointer
textIndex--; // as we incrementing the text pointer outside but we should, in fact, stay at the current text index
patternIndex++;
} else {
return false;
}
textIndex++;
patternIndex++;
}
return true;
}
public static void main(String[] args) {
System.out.println(isMatch("aa", "a")); // output: false
System.out.println(isMatch("aa", "aa")); // output: true
System.out.println(isMatch("abc", "a.c")); // output: true
System.out.println(isMatch("abbb", "ab*")); // output: true
System.out.println(isMatch("acd", "ab*c.")); // output: true
System.out.println(isMatch("abbdbb", "ab*d")); // output: false
System.out.println(isMatch("aba", "a.a")); // output: true
System.out.println(isMatch("acd", "ab*c.")); // output: true
System.out.println(isMatch("abaa", "a.*a*")); // output: true
}
}