fork download
  1. #include <regex>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. int main()
  6. {
  7. std::string line = "abcde";
  8. {
  9. const std::string RX_ION_TYPE("ab?|ab?(?:cd|dc)");
  10.  
  11. const auto regexType = std::regex::ECMAScript;
  12.  
  13. std::regex rx_ionType;
  14.  
  15. rx_ionType.assign(
  16. "(" + RX_ION_TYPE + ")"
  17. , regexType);
  18.  
  19. std::smatch match;
  20.  
  21. if (std::regex_search(line, match, rx_ionType))
  22. {
  23. for (int i = 0; i < match.size(); i++)
  24. {
  25. std::cout << "|" << match.str(i) << "|\n";
  26. }
  27.  
  28. }
  29. else
  30. {
  31. std::cout << "No match.\n";
  32. }
  33. }
  34.  
  35. {
  36. const std::string RX_ION_TYPE("ab?(?:cd|dc)|ab?");
  37.  
  38. const auto regexType = std::regex::ECMAScript;
  39.  
  40. std::regex rx_ionType;
  41.  
  42. rx_ionType.assign(
  43. "(" + RX_ION_TYPE + ")"
  44. , regexType);
  45.  
  46. std::smatch match;
  47.  
  48. if (std::regex_search(line, match, rx_ionType))
  49. {
  50. for (int i = 0; i < match.size(); i++)
  51. {
  52. std::cout << "|" << match.str(i) << "|\n";
  53. }
  54.  
  55. }
  56. else
  57. {
  58. std::cout << "No match.\n";
  59. }
  60. }
  61. {
  62. const std::string RX_ION_TYPE("ab?(?:cd|dc)?");
  63.  
  64. const auto regexType = std::regex::ECMAScript;
  65.  
  66. std::regex rx_ionType;
  67.  
  68. rx_ionType.assign(
  69. "(" + RX_ION_TYPE + ")"
  70. , regexType);
  71.  
  72. std::smatch match;
  73.  
  74. if (std::regex_search(line, match, rx_ionType))
  75. {
  76. for (int i = 0; i < match.size(); i++)
  77. {
  78. std::cout << "|" << match.str(i) << "|\n";
  79. }
  80.  
  81. }
  82. else
  83. {
  84. std::cout << "No match.\n";
  85. }
  86. }
  87.  
  88. return 0;
  89. }
  90.  
Success #stdin #stdout 0s 3544KB
stdin
Standard input is empty
stdout
|ab|
|ab|
|abcd|
|abcd|
|abcd|
|abcd|