fork download
  1. #include <iostream>
  2. #include <regex.h>
  3. #include <string.h>
  4.  
  5. #define REGEX "@([^(@]+)(\\(\\(property\\(([^)]+)\\)val\\(([^)]+)\\)\\))?"
  6. #define DEBUG 1
  7. using namespace std;
  8.  
  9. typedef struct {
  10. char* name;
  11. char* property;
  12. char* value;
  13. } item;
  14.  
  15. int getMatches(char* haystack, item** items){
  16. int l = -1, ats = 0;
  17. while (haystack[++l])
  18. if (haystack[l] == '@')
  19. ats++;
  20. *items = (item*) malloc(ats * sizeof(item));
  21. item* arr = *items;
  22. regex_t needle;
  23. regcomp(&needle, REGEX, REG_ICASE|REG_EXTENDED);
  24. regmatch_t match[5];
  25. int ret;
  26. int x = -1;
  27. while (!(ret = regexec(&needle, haystack, 5, match,0))){
  28. ++x;
  29. if (DEBUG) cerr << "MATCH INSTANCE " << x << "\n";
  30.  
  31. //Get the name
  32. int bufsize = match[1].rm_eo-match[1].rm_so + 1;
  33. arr[x].name = (char *) malloc(bufsize);
  34. if(arr[x].name == NULL)
  35. cerr << "MALLOC FAILED\n";
  36. strncpy(arr[x].name, &(haystack[match[1].rm_so]), bufsize - 1);
  37. arr[x].name[bufsize-1]=0x0;
  38. if (DEBUG) cerr <<" "<<arr[x].name <<'\n';
  39. //if property and value are both valid...
  40. if (!(match[3].rm_so > l || match[3].rm_so<0 || match[3].rm_eo > l || match[3].rm_so< 0
  41. || match[4].rm_so > l || match[4].rm_so<0 || match[4].rm_eo > l || match[4].rm_so< 0)){
  42. //Get the property
  43. bufsize = match[3].rm_eo-match[3].rm_so + 1;
  44. arr[x].property = (char *) malloc(bufsize);
  45. if(arr[x].property == NULL)
  46. cerr << "MALLOC FAILED\n";
  47. strncpy(arr[x].property, &(haystack[match[3].rm_so]), bufsize - 1);
  48. arr[x].property[bufsize-1]=0x0;
  49. if (DEBUG) cerr <<" "<<arr[x].property<<'\n';
  50.  
  51. //Get the Value
  52. bufsize = match[4].rm_eo-match[4].rm_so + 1;
  53. arr[x].value = (char *) malloc(bufsize);
  54. if(arr[x].value == NULL)
  55. cerr << "MALLOC FAILED\n";
  56. strncpy(arr[x].value, &(haystack[match[4].rm_so]), bufsize - 1);
  57. arr[x].value[bufsize-1]=0x0;
  58. if (DEBUG) cerr <<" "<<arr[x].value<<'\n';
  59. } else {
  60. arr[x].property = NULL;
  61. arr[x].value = NULL;
  62. }
  63. haystack = &(haystack[match[0].rm_eo]);
  64. l -= match[0].rm_eo;
  65. }
  66. return x+1;
  67. }
  68.  
  69. int main() {
  70. char* haystack = "MyCarGarage@Mustang((property(PS)val(500))@Porsche((property(PS)val(425))@Corvette@Corsair@Pinto((property(exploded)val(not yet))";
  71. item* matches;
  72. int n = getMatches(haystack, &matches);
  73. cout << "FOUND " << n << " MATCHES:\n\n";
  74. for (int i = 0; i<n; i++){
  75. cout <<'S'<< i << ": "<< matches[i].name<<'\n';
  76. if (matches[i].property != NULL){
  77. cout <<'P'<< i << ": "<< matches[i].property<<'\n';
  78. cout <<'V'<< i << ": "<< matches[i].value<<'\n'; //*/
  79. }
  80. cout <<'\n';
  81. //cout << other[0].name <<'\n';
  82. }
  83. return 0;
  84. }
Success #stdin #stdout #stderr 0s 2988KB
stdin
Standard input is empty
stdout
FOUND 5 MATCHES:

S0: Mustang
P0: PS
V0: 500

S1: Porsche
P1: PS
V1: 425

S2: Corvette

S3: Corsair

S4: Pinto
P4: exploded
V4: not yet

stderr
MATCH INSTANCE 0
    Mustang
    PS
    500
MATCH INSTANCE 1
    Porsche
    PS
    425
MATCH INSTANCE 2
    Corvette
MATCH INSTANCE 3
    Corsair
MATCH INSTANCE 4
    Pinto
    exploded
    not yet