| 1 | |
package liquibase.util; |
| 2 | |
|
| 3 | |
import java.util.regex.Matcher; |
| 4 | |
import java.util.regex.Pattern; |
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | 1 | public class RegexMatcher { |
| 12 | |
private String text; |
| 13 | |
private Pattern[] patterns; |
| 14 | |
private boolean allMatched; |
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | 7 | public RegexMatcher(String text, String[] regexToMatch) { |
| 25 | 7 | assert text != null && regexToMatch != null; |
| 26 | |
|
| 27 | 7 | this.text = text; |
| 28 | 7 | patterns = new Pattern[regexToMatch.length]; |
| 29 | 20 | for (int i = 0; i < regexToMatch.length; i++) { |
| 30 | 14 | patterns[i] = Pattern.compile(regexToMatch[i], Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); |
| 31 | |
} |
| 32 | |
|
| 33 | 6 | allMatched = checkMatchingInSequentialOrder(); |
| 34 | 6 | } |
| 35 | |
|
| 36 | |
private boolean checkMatchingInSequentialOrder() { |
| 37 | 6 | int index = 0; |
| 38 | 16 | for (Pattern p : patterns) { |
| 39 | 12 | Matcher m = p.matcher(text.substring(index)); |
| 40 | 12 | if (!m.find()) |
| 41 | 2 | return false; |
| 42 | |
else |
| 43 | 10 | index += m.end(); |
| 44 | |
} |
| 45 | 4 | return true; |
| 46 | |
} |
| 47 | |
|
| 48 | |
public boolean allMatchedInSequentialOrder() { |
| 49 | 6 | return allMatched; |
| 50 | |
} |
| 51 | |
|
| 52 | |
} |