fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <regex>
  4. #include <string>
  5. #include <utility>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. enum POSES // Deliminators
  11. {
  12. EQUALS,
  13. OPEN,
  14. CLOSE,
  15. SPACE,
  16. EOL,
  17. RETURN,
  18. TAB,
  19. COMMENT,
  20. POSES_SIZE
  21. };
  22.  
  23. int main() {
  24. const auto input = "#Comment\n\nshow_position = { x=-9 y =78 }"s;
  25. vector<pair<POSES, string>> tokens;
  26. const regex re{ "\\s*(?:\n|(#[^\n]*)|(\\{)|(\\})|(=)|([^{}= \t\r\n]+))" };
  27.  
  28. for_each(sregex_iterator(cbegin(input), cend(input), re), sregex_iterator(), [&](const auto& i) {
  29. if (i[1].length() > 0U) {
  30. tokens.emplace_back(COMMENT, i[1]);
  31. } else if (i[2].length() > 0U) {
  32. tokens.emplace_back(OPEN, "{"s);
  33. } else if (i[3].length() > 0U) {
  34. tokens.emplace_back(CLOSE, "}"s);
  35. } else if (i[4].length() > 0U) {
  36. tokens.emplace_back(EQUALS, "="s);
  37. } else if (i[5].length() > 0U) {
  38. tokens.emplace_back(POSES_SIZE, i[5]);
  39. }
  40. });
  41.  
  42. for (const auto& i : tokens) {
  43. switch (i.first) {
  44. case EQUALS:
  45. cout << "EQUALS: =\n";
  46. break;
  47. case OPEN:
  48. cout << "OPEN: {\n";
  49. break;
  50. case CLOSE:
  51. cout << "CLOSE: }\n";
  52. break;
  53. case COMMENT:
  54. cout << "COMMENT: " << i.second << endl;
  55. break;
  56. case POSES_SIZE:
  57. cout << "POSES_SIZE: " << i.second << endl;
  58. }
  59. }
  60. }
Success #stdin #stdout 0s 3500KB
stdin
Standard input is empty
stdout
COMMENT:    #Comment
POSES_SIZE: show_position
EQUALS:     =
OPEN:       {
POSES_SIZE: x
EQUALS:     =
POSES_SIZE: -9
POSES_SIZE: y
EQUALS:     =
POSES_SIZE: 78
CLOSE:      }