删除包含特定模式的子字符串,并以特定字符结尾

时间:2016-02-03 07:35:29

标签: c++ regex substring

例如,我有一个如下字符串:

  

“VISITOR_INFO1_LIVE = USv90B-7CzI; LOGIN_INFO = e486e37a395be3f0e3b3237d090a6829c1oAAAB7IjQiOiAiREVMRUdBVEVEIiwgIjciOiAwLCAiMSI6IDEsICIzIjogMjAxMTk0MTMwNiwgIjgiOiA2MDgwMTg0NTEzNjQsICIxMCI6IDIzOTYyMTEyODczNH0 =; PREF = F5 = 30; HSID = AHuJQBOVR0lQoRt_3; APISID = QaParXGsQcEPCzKg / A1smCfYrfMjxvfEPT; YSC = Vm3Amq5loFM”;

我想删除包含* SID(此处为HSID,APISID)的所有模式到';'。我还想删除子字符串“LOGIN_INFO = ....;”

因此,输出字符串应为"VISITOR_INFO1_LIVE=L80EDuHCEF8; PREF=f5=30";

以下是我提出的解决方案,但我认为可以改进性能:

const char *str ="VISITOR_INFO1_LIVE=USv90B-7CzI; LOGIN_INFO=e486e37a395be3f0e3b3237d090a6829c1oAAAB7IjQiOiAiREVMRUdBVEVEIiwgIjciOiAwLCAiMSI6IDEsICIzIjogMjAxMTk0MTMwNiwgIjgiOiA2MDgwMTg0NTEzNjQsICIxMCI6IDIzOTYyMTEyODczNH0=; PREF=f5=30;HSID=AHuJQBOVR0lQoRt_3; APISID=QaParXGsQcEPCzKg/A1smCfYrfMjxvfEPT; YSC=Vm3Amq5loFM"; 
     char *Cookie = NULL;
     cout << "original string is:\n" << str << "\n";
     int len = strlen(str)+1;
     cout << "length of original string is : " << len << "\n";
     Cookie = new char[strlen(str)];
     strncpy(Cookie,str,len);

     char *p1 = strstr(Cookie,"LOGIN_INFO");
     char *p2 = NULL;
     if(p1){
          p2 = strstr(p1,";")+1;
          while(*p2 == ' ') p2++;
     }
     if(p1 && p2)
          memmove(p1,p2,strlen(p2)+1);

     char *ID = strstr(Cookie,"SID"); 
     while( ID != NULL){
          char *start_pos = NULL, *end_pos = NULL;    
          while((*ID != ';') && (*ID != Cookie[0]) && (*ID != ' ')){
               --ID;
          }
          if(*ID == Cookie[0]) start_pos = ID;
          else start_pos = ID+1;
          end_pos = strstr(start_pos,";")+1;
          while(*end_pos == ' ')
               end_pos++;
          memmove(start_pos,end_pos,strlen(end_pos)+1);                                   
        //  } 
          /*else
               std::cout << "does not find substr " << "\n";*/

        //  cout << "modified string is :" << Cookie << "\n";
          ID = strstr(Cookie,"SID");
     }
          //cout << "final modified string is : " << Cookie << "\n";
     char *Cookie_modified = NULL;
     const char *pch = strstr(Cookie,"PREF");
     if(pch != NULL){
          const char *append = "&f2=8000000";
          int len = strlen(Cookie) + strlen(append) + 1;
          Cookie_modified = new char[len];
          strncpy(Cookie_modified,Cookie,len);
          Cookie_modified[len-1] = '\0';
          char *p = strstr(Cookie_modified,"PREF");
          strncpy(p+(strlen(p)),append,strlen(append));
          cout << "modified Cookie is : " << Cookie_modified << "\n";
      //    cout << "length of modified cookie is : " << strlen(Cookie_modified) << "\n";
     }

     else{
          cout << "do not find reference: " << "\n";
          const char *append = ";PERF=f2=8000000";
          int len = strlen(Cookie) + strlen(append) + 1;
          Cookie_modified = new char[len];
          Cookie_modified[len-1] = '\0';
          strcat(Cookie_modified,Cookie);
          strcat(Cookie_modified,append);
          cout << "case 2: modified Cookie is: " << Cookie_modified << "\n";

     }
     delete[] Cookie;
     delete[] Cookie_modified;     
     return 0;
}

1 个答案:

答案 0 :(得分:0)

只需使用以下正则表达式std::regex_replace

([^\s]*SID[^;]*;|[^\s]*LOGIN_INFO[^;]*;)

用空字符串替换。

Live Demo