前端之家收集整理的这篇文章主要介绍了
正则表达式匹配,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
模式匹配
public class Solution {
public boolean match(char[] str,char[] pattern)
{
return new String(str).matches(new String(pattern));
}
public boolean match2(char[] str,char[] pattern) {
if (str == null || pattern == null) {
return false;
}
int strIndex = 0;
int patternIndex = 0;
return matchCore(str,strIndex,pattern,patternIndex);
}
public boolean matchCore(char[] str,int strIndex,char[] pattern,int patternIndex) {
if (strIndex == str.length && patternIndex == pattern.length) {
return true;
}
if (strIndex != str.length && patternIndex == pattern.length) {
return false;
}
if (patternIndex + 1 < pattern.length && pattern[patternIndex + 1] == '*') {
if ((strIndex != str.length && pattern[patternIndex] == str[strIndex]) || (pattern[patternIndex] == '.' && strIndex != str.length)) {
return matchCore(str,patternIndex + 2)
|| matchCore(str,strIndex + 1,patternIndex + 2)
|| matchCore(str,patternIndex);
} else {
return matchCore(str,patternIndex + 2);
}
}
if ((strIndex != str.length && pattern[patternIndex] == str[strIndex]) || (pattern[patternIndex] == '.' && strIndex != str.length)) {
return matchCore(str,patternIndex + 1);
}
return false;
}
}
原文链接:https://www.f2er.com/regex/357485.html