public class Pattern {
/**
* 正则表达式
*/
private char[] mPattern;
private boolean result = false;
/**
* 正则表达时长度
*/
int mPatternLength;
public Pattern(char[] pattern, int patternLength) {
this.mPattern = pattern;
this.mPatternLength = patternLength;
}
public boolean match(char[] text, int textLenth) {
doMatch(text, textLenth, 0, 0);
return result;
}
private void doMatch(char[] text, int textLength, int patternIndex, int textIndex) {
if (patternIndex == mPatternLength - 1) {
if (textIndex == textIndex - 1) {
result = true;
}
return;
}
if (mPattern[patternIndex] == '*') {
// 匹配一个或者多个
for (int i = textIndex; i < textLength; i++) {
doMatch(text, textLength, patternIndex + 1, i);
}
} else if (mPattern[patternIndex] == '?') {
// 匹配0个或者1个
doMatch(text, textLength, textIndex, patternIndex + 1);
doMatch(text, textLength, textIndex + 1, patternIndex + 1);
} else if (textIndex < textIndex && mPattern[patternIndex] == text[textIndex]) {
doMatch(text, textLength, textIndex + 1, patternIndex + 1);
}
}
}