fork download
  1. #include <iostream>
  2. #include <regex.h>
  3. #include <string.h>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. char* haystack = "MyCarGarage@Mustang((property(PS)val(500))@Porsche((property(PS)val(425))@Corvette@Corsair@Pinto((property(exploded)val(not yet))";
  9. regex_t needle;
  10. regcomp(&needle, "@([^(@]+)(\\(\\(property\\(([^)]+)\\)val\\(([^)]+)\\))?", REG_ICASE|REG_EXTENDED);
  11. regmatch_t match[5];
  12. int ret;
  13. int x = 1;
  14. while (!(ret = regexec(&needle, haystack, 5, match,0))){
  15. cerr << "MATCH INSTANCE " << x++ << "\n";
  16. for (int i=0; i<5; i++){
  17. if (i==2) continue;
  18. cerr << " match["<<i<<"]: "<< match[i].rm_so << " to " << match[i].rm_eo << "\n ";
  19. int l = strlen(haystack);
  20. if (!(match[i].rm_so > l || match[i].rm_so<0 || match[i].rm_eo > l || match[i].rm_so< 0)){
  21. for (int j= match[i].rm_so; j<match[i].rm_eo; j++){
  22. cerr << haystack[j];
  23. if(i) cout << haystack[j];
  24. }
  25. }
  26. if(i && i != 4) cout<<",";
  27. cerr <<"\n";
  28. }
  29. cout <<"\n";
  30. haystack = &haystack[match[0].rm_eo];
  31. }
  32. return 0;
  33. }
Success #stdin #stdout #stderr 0s 2984KB
stdin
Standard input is empty
stdout
Mustang,PS,500
Porsche,PS,425
Corvette,,
Corsair,,
Pinto,exploded,not yet
stderr
MATCH INSTANCE 1
  match[0]: 11 to 41
    @Mustang((property(PS)val(500)
  match[1]: 12 to 19
    Mustang
  match[3]: 30 to 32
    PS
  match[4]: 37 to 40
    500
MATCH INSTANCE 2
  match[0]: 1 to 31
    @Porsche((property(PS)val(425)
  match[1]: 2 to 9
    Porsche
  match[3]: 20 to 22
    PS
  match[4]: 27 to 30
    425
MATCH INSTANCE 3
  match[0]: 1 to 10
    @Corvette
  match[1]: 2 to 10
    Corvette
  match[3]: -1 to -1
    
  match[4]: -1 to -1
    
MATCH INSTANCE 4
  match[0]: 0 to 8
    @Corsair
  match[1]: 1 to 8
    Corsair
  match[3]: -1 to -1
    
  match[4]: -1 to -1
    
MATCH INSTANCE 5
  match[0]: 0 to 38
    @Pinto((property(exploded)val(not yet)
  match[1]: 1 to 6
    Pinto
  match[3]: 17 to 25
    exploded
  match[4]: 30 to 37
    not yet