-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegularExpressionMatcher.java
More file actions
66 lines (48 loc) · 1.94 KB
/
RegularExpressionMatcher.java
File metadata and controls
66 lines (48 loc) · 1.94 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
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegularExpressionMatcher {
protected String regularExpressionString;
protected static ArrayList<String> wordsToCompareAgainst;
protected static Pattern pattern;
public RegularExpressionMatcher() {
regularExpressionString = "";
wordsToCompareAgainst = null;
}
public void setRegularExpressionString(String regexInput) {
regularExpressionString = regexInput;
pattern = Pattern.compile(regularExpressionString);
}
public String getRegularExpressionString() {
return regularExpressionString;
}
public void setWordsToCompareAgainst(ArrayList<String> wordsInput) {
wordsToCompareAgainst = wordsInput;
}
public static void main(String[] args) throws Exception {
RegularExpressionMatcherTest testObj = new RegularExpressionMatcherTest();
testObj.setUp();
testObj.test_language1();
findMatches();
testObj.test_language2();
findMatches();
testObj.test_language3();
findMatches();
}
public static ArrayList<String> findMatches() {
// TODO: Given the ArrayList wordsToCompareAgainst
// and the Pattern variable named pattern representing
// a regular expression, add to the ArrayList matchingList
// the words that are in the wordsToCompareAgainst list that "match"
// (could have been generated from) the regular expression
ArrayList<String> matchingList = new ArrayList<String>();
for(int i=0; i<wordsToCompareAgainst.size();i++){
if(pattern.matcher(wordsToCompareAgainst.get(i)).matches() == true){
matchingList.add( wordsToCompareAgainst.get(i));
}
}
// wordsToCompareAgainst has been filled with words via a separate
// method
return matchingList;
}
}