Regular Expression Matching 正则匹配问题

前端之家收集整理的这篇文章主要介绍了Regular Expression Matching 正则匹配问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Implement regular expression matching with support for'.'and'*'.

  1. '.' Matches any single character.
  2. '*' Matches zero or more of the preceding element.
  3.  
  4. The matching should cover the entire input string (not partial).
  5.  
  6. The function prototype should be:
  7. bool isMatch(const char *s,const char *p)
  8.  
  9. Some examples:
  10. isMatch("aa","a") false
  11. isMatch("aa","aa") true
  12. isMatch("aaa","aa") false
  13. isMatch("aa","a*") true
  14. isMatch("aa",".*") true
  15. isMatch("ab",".*") true
  16. isMatch("aab","c*a*b") true
  1. class Solution {
  2. public:
  3. bool isMatch(const char *s,const char *p) {
  4. // Note: The Solution object is instantiated only once and is reused by each test case.
  5. if(p == NULL)
  6. return s!=NULL?false:true;
  7. if(*p == '\0')
  8. return *s == '\0';
  9. if(*(p+1) == '*')
  10. {
  11. while(*s==*p || (*p=='.' && (*s)!='\0') )
  12. {
  13. if(isMatch(s,p+2) )
  14. return true;
  15. ++s;
  16. }
  17. return isMatch(s,p+2);
  18. }
  19. else
  20. {
  21. if(*s==*p || (*p=='.'&&(*s)!='\0') )
  22. return isMatch(s+1,p+1);
  23. else
  24. return false;
  25. }
  26. }
  27. };

参考:

http://www.jb51.cc/article/p-vdzqmsnc-bap.html

猜你在找的正则表达式相关文章