zoukankan      html  css  js  c++  java
  • c++删除指定字符串之间的内容(比正则表达式快几十倍)[转]

    为了能够让用户更清晰的写采集规则,特意把采集回来的源码经过精简之后再进行处理,比如,要

     

    删除 <style> </style> <script> </script>注释等内容去除标签垃圾属性等,使用正则表达式,大一点的网页

     

    数据,匹配替换为空一次竟然可以达到几百毫秒,吓死人,故自己写了个函数,同样的数据大小1-3毫秒完成.

     

    Cpp代码  收藏代码
    1. <span><span style="color: #000000;">  
    2.   
    3. //------这上面几段垃圾html代码删不掉了,请无视它,自动跑进去的  
    4.   
    5. class BaseLib  
    6. {  
    7. public:  
    8.     BaseLib();  
    9.     static QString removeOf(QString strData,QString strBegin,QString strEnd,bool bIgnoreCase = false,bool bAllStr = false);  
    10. };  
    11.   
    12. </span>  
    13.   
    14.   
    15.   
    16. </span>  
    17.   
    18.   
    19.   
    20. QString BaseLib::removeOf(QString strData,QString strBegin,QString strEnd,bool bIgnoreCase,bool bAllStr)  
    21. {  
    22.     int nPos = 0;  
    23.   
    24.     int nStartPos = -1;  
    25.     int nEndPos = -1;  
    26.     int nBeginLen = strBegin.length();  
    27.     int nEndLen = strEnd.length();  
    28.   
    29.     if(bIgnoreCase == true){  
    30.         QString strNewData = strData.toUpper();  
    31.         strBegin = strBegin.toUpper();  
    32.         strEnd = strEnd.toUpper();  
    33.         while(1){  
    34.             nStartPos = strNewData.indexOf(strBegin,nPos);  
    35.             if(nStartPos == -1){  
    36.                 break;  
    37.             }  
    38.             nEndPos = strNewData.indexOf(strEnd,nStartPos + nBeginLen);  
    39.             if(nEndPos == -1){  
    40.                 break;  
    41.             }  
    42.   
    43.             nEndPos = nEndPos - nStartPos + nEndLen;  
    44.   
    45.             if(bAllStr == false){  
    46.                 strNewData.remove(nStartPos + nBeginLen,nEndPos - nBeginLen - nEndLen);  
    47.                 strData.remove(nStartPos + nBeginLen,nEndPos - nBeginLen - nEndLen);  
    48.                 nPos = nStartPos + nEndPos - nBeginLen - nEndLen;  
    49.             }else{  
    50.                 strNewData.remove(nStartPos,nEndPos);  
    51.                 strData.remove(nStartPos,nEndPos);  
    52.                 nPos = nStartPos;  
    53.             }  
    54.         }  
    55.     }else{  
    56.         while(1){  
    57.             nStartPos = strData.indexOf(strBegin,nPos);  
    58.             if(nStartPos == -1){  
    59.                 break;  
    60.             }  
    61.             nEndPos = strData.indexOf(strEnd,nStartPos + nBeginLen);  
    62.             if(nEndPos == -1){  
    63.                 break;  
    64.             }  
    65.   
    66.             nEndPos = nEndPos - nStartPos + nEndLen;  
    67.   
    68.             if(bAllStr == false){  
    69.                 strData.remove(nStartPos + nBeginLen,nEndPos - nBeginLen - nEndLen);  
    70.                 nPos = nStartPos + nEndPos - nBeginLen - nEndLen;  
    71.             }else{  
    72.                 strData.remove(nStartPos,nEndPos);  
    73.                 nPos = nStartPos;  
    74.             }  
    75.         }  
    76.     }  
    77.     return strData;  
    78. }  
     

    参数1 :传递数据

     

    参数2 :传递要处理的数据开头

     

    参数3 :传递要处理的数据结尾

     

    参数4 :bool值,如果填写true则忽略大小写,false,不忽略大小写(速度更快点),可以不填写,默认false

     

    参数5 :是否只删除开头和结尾之间的数据,true 删除包含数据开头和数据结尾的数据,false 保留数据开头和结尾

     

    (参数4,5可不填写)

     

    例子:

     

    Cpp代码  收藏代码
    1. QString str = "<head><title>this is a test</title></head>";  
    2.   
    3. QString strData = BaseLib::removeOf(strData,"<title>","</title>",false,true);  

     

    返回:

     

    Cpp代码  收藏代码
    1. <head></head>  

     

    字符串处理使用的QT的QString 库,如果用MFC的人,可以替换成CString

     

    c++标准库string版(比QString稍微快点)

    Cpp代码  收藏代码
    1. string BaseLib::removeOf(string strData,string strBegin,string strEnd,bool bIgnoreCase,bool bAllStr)  
    2. {  
    3.     int nPos = 0;  
    4.     int nStartPos = -1;  
    5.     int nEndPos = -1;  
    6.     int nBeginLen = strBegin.length();  
    7.     int nEndLen = strEnd.length();  
    8.   
    9.     if(bIgnoreCase == true){  
    10.         string strNewData = strData;  
    11.         for(int i=0;i<(int)strNewData.size();++i){  
    12.             strNewData[i]=toupper(strNewData[i]);  
    13.         }  
    14.         for(int i=0;i<(int)strBegin.size();++i){  
    15.             strBegin[i]=toupper(strBegin[i]);  
    16.         }  
    17.         for(int i=0;i<(int)strEnd.size();++i){  
    18.             strEnd[i]=toupper(strEnd[i]);  
    19.         }  
    20.   
    21.         while(1){  
    22.             nStartPos = strNewData.find(strBegin,nPos);  
    23.             if(nStartPos == -1){  
    24.                 break;  
    25.             }  
    26.             nEndPos = strNewData.find(strEnd,nStartPos + nBeginLen);  
    27.             if(nEndPos == -1){  
    28.                 break;  
    29.             }  
    30.   
    31.             nEndPos = nEndPos - nStartPos + nEndLen;  
    32.   
    33.             if(bAllStr == false){  
    34.                 strNewData.erase(nStartPos + nBeginLen,nEndPos - nBeginLen - nEndLen);  
    35.                 strData.erase(nStartPos + nBeginLen,nEndPos - nBeginLen - nEndLen);  
    36.                 nPos = nStartPos + nEndPos - nBeginLen - nEndLen;  
    37.             }else{  
    38.                 strNewData.erase(nStartPos,nEndPos);  
    39.                 strData.erase(nStartPos,nEndPos);  
    40.                 nPos = nStartPos;  
    41.             }  
    42.         }  
    43.     }else{  
    44.         while(1){  
    45.             nStartPos = strData.find(strBegin,nPos);  
    46.             if(nStartPos == -1){  
    47.                 break;  
    48.             }  
    49.             nEndPos = strData.find(strEnd,nStartPos + nBeginLen);  
    50.             if(nEndPos == -1){  
    51.                 break;  
    52.             }  
    53.   
    54.             nEndPos = nEndPos - nStartPos + nEndLen;  
    55.   
    56.             if(bAllStr == false){  
    57.                 strData.erase(nStartPos + nBeginLen,nEndPos - nBeginLen - nEndLen);  
    58.                 nPos = nStartPos + nEndPos - nBeginLen - nEndLen;  
    59.             }else{  
    60.                 strData.erase(nStartPos,nEndPos);  
    61.                 nPos = nStartPos;  
    62.             }  
    63.         }  
    64.     }  
    65.     return strData;  
    66. }  
     

     

  • 相关阅读:
    .NET_.NET 发布(publish)网站_012
    Oracle 11g Release 1 (11.1) 单行函数——日期函数
    Oracle 11g Release 1 (11.1) SQL_层级查询(概)
    C# 清理非托管资源
    NChain 0.1 项目——但愿是根救命稻草
    人工智能——神经网络
    概念——都有哪些 Web 服务方式
    从 Fibonacci 数列看“动态规划”思想
    QR 码
    Oracle 11g Release 1 (11.1) 表空间——创建和扩展永久表空间
  • 原文地址:https://www.cnblogs.com/rooney/p/2720158.html
Copyright © 2011-2022 走看看