Advertisement
zhangsongcui

Compilable Regex Version

Sep 12th, 2011
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. template <const char Expression[], char C = Expression[0], size_t index = 0>
  6. struct myRegex
  7. {
  8.     myRegex(string&& _str): str(_str) {}
  9.  
  10.     bool isMatch()
  11.     {
  12.         return str.front()==C ? myRegex<Expression, Expression[index+1], index+1>(str.substr(1)).isMatch() : false;
  13.     }
  14.  
  15. private:
  16.     string str;
  17. };
  18.  
  19. template <const char Expression[], size_t index>
  20. struct myRegex<Expression, '?', index>
  21. {
  22.     myRegex(string&& _str): str(_str) {}
  23.  
  24.     bool isMatch()
  25.     {
  26.         return myRegex<Expression, Expression[index+1], index+1>(str.substr(1)).isMatch();
  27.     }
  28.  
  29. private:
  30.     string str;
  31. };
  32.  
  33. template <const char Expression[], size_t index>
  34. struct myRegex<Expression, '\0', index>
  35. {
  36.     myRegex(string&& _str): str(_str) {}
  37.  
  38.     bool isMatch()
  39.     {
  40.         return true;
  41.     }
  42.  
  43. private:
  44.     string str;
  45. };
  46.  
  47. extern constexpr char Expression[] = "a?b";
  48. int main(void)
  49. {
  50.     cout << myRegex<Expression>("a+b").isMatch();
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement