【正则表达式判断】Regular Expression Matching
2014-04-06 16:37
381 查看
Implement regular expression matching with support for
参考:http://blog.csdn.net/fightforyourdream/article/details/17717873
public class Solution {
public boolean isMatch(String s, String p, int sp, int pp){
int ls = s.length();
int lp = p.length();
if(pp >= lp) return sp>=ls;
if(pp == lp-1) return (sp==ls-1) && (s.charAt(sp)==p.charAt(pp) || p.charAt(pp)=='.');
if(pp+1 < lp && p.charAt(pp + 1) != '*'){//如果下一个字符不为*,则比较s和p当前的字符
if((sp!=ls) && (s.charAt(sp)==p.charAt(pp) || p.charAt(pp)=='.'))
return isMatch(s, p, sp+1, pp+1);
else return false;
}
else{//若下一个字符为*
while((sp<ls) && (s.charAt(sp)==p.charAt(pp) || p.charAt(pp)=='.')){
if(isMatch(s, p, sp, pp+2)) return true;//如果把当前s中字符放入后续匹配成功,即p的下一*没有用
sp++;//不成功则将*执行一次
}
return isMatch(s, p, sp, pp+2);
}
}
public boolean isMatch(String s, String p) {
return isMatch(s, p, 0, 0);
}
}
'.'and
'*'.
'.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true
参考:http://blog.csdn.net/fightforyourdream/article/details/17717873
public class Solution {
public boolean isMatch(String s, String p, int sp, int pp){
int ls = s.length();
int lp = p.length();
if(pp >= lp) return sp>=ls;
if(pp == lp-1) return (sp==ls-1) && (s.charAt(sp)==p.charAt(pp) || p.charAt(pp)=='.');
if(pp+1 < lp && p.charAt(pp + 1) != '*'){//如果下一个字符不为*,则比较s和p当前的字符
if((sp!=ls) && (s.charAt(sp)==p.charAt(pp) || p.charAt(pp)=='.'))
return isMatch(s, p, sp+1, pp+1);
else return false;
}
else{//若下一个字符为*
while((sp<ls) && (s.charAt(sp)==p.charAt(pp) || p.charAt(pp)=='.')){
if(isMatch(s, p, sp, pp+2)) return true;//如果把当前s中字符放入后续匹配成功,即p的下一*没有用
sp++;//不成功则将*执行一次
}
return isMatch(s, p, sp, pp+2);
}
}
public boolean isMatch(String s, String p) {
return isMatch(s, p, 0, 0);
}
}
相关文章推荐
- 判断正则表达式是否匹配 Regular Expression Matching @LeetCode
- 简单的正则表达式匹配 Regular Expression Matching
- 多正则表达式匹配(Multiple Regular Expression Matching)
- LeetCode OJ:Regular Expression Matching(正则表达式匹配)
- [LeetCode] Regular Expression Matching 正则表达式匹配
- 10.正则表达式匹配(Regular Expression Matching)
- Regular Expression Matching,regex,正则表达式匹配,利用动态规划
- LeetCode OJ 之 Regular Expression Matching (正则表达式匹配)
- 正则表达式匹配(Regular Expression Matching)
- [Leetcode] Regular expression matching 正则表达式匹配
- LeetCode 10 Regular Expression Matching (正则表达式匹配)
- leetcode Wildcard Matching ,Regular Expression Matching (正则表达式匹配和通配符匹配)
- [leetcode]Regular Expression Matching(正则表达式的匹配)
- LeetCode-10-Regular-Expression-Matching 递归模拟正则表达式
- leetcode(10) - Regular Expression Matching 正则表达式匹配
- LeetCode正则表达式-Regular Expression Matching
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
- 正则表达式匹配-leetcode Regular Expression Matching
- [置顶] 通配符匹配(Wildcard Matching)与正则表达式匹配(Regular Expression Matching)解法解析
- 多正则表达式匹配 (Multiple Regular Expression Matching) 中的动态 DFA 算法