<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          數(shù)據(jù)處理 | 你不得不會的「正則表達式」

          共 345字,需瀏覽 1分鐘

           ·

          2020-12-23 21:21

          若要判斷一個輸入的QQ號是否有效,你會如何處呢?

          首先你得分析一下其對應(yīng)規(guī)則,依次列出:

          1. 長度大于5,小于等于11;

          2. 首位不能為0;

          3. 是否為純數(shù)字?

          規(guī)則既列,接著就該嘗試實現(xiàn)了,那么用什么來表示字符串呢?在C++中,最容易想到的就是string了,其中提供了許多成員函數(shù)可以處理字符串,所以有了如下實現(xiàn):

           1std::string?qq;
          2std::cin?>>?qq;
          3
          4//?1.?判斷位數(shù)是否合法
          5if?(qq.length()?>=?5?&&?qq.length()?<=?11)
          6{
          7????//?2.?判斷是否非'0'開頭
          8????if?(qq[0]?!=?'0')
          9????{
          10????????//?3.?判斷是否為純數(shù)字
          11????????auto?pos?=?std::find_if(qq.begin(),?qq.end(),?[](const?char&?ch)?{
          12????????????return?ch?'0'?||?ch?>?'9';
          13????????});
          14????????if?(pos?==?qq.end())
          15????????????std::cout?<"valid.\n";
          16????}
          17}

          雖然寫出來了,但是有沒有感到異常繁瑣?這還僅僅是一個對應(yīng)規(guī)則較少的處理,便如此麻煩,若是要檢測IP地址、身份證號,或是解析一段HTML數(shù)據(jù),或是其它更復(fù)雜的字串,那豈非更令人叫苦不迭?

          當(dāng)然,也有許多擴展庫對字符串處理提供了方便,其中比較好用的是boost中的string_algo庫(已于C++17納入了標準庫,并改名為string_view),但本篇主要說C++11的regex庫,其對復(fù)雜數(shù)據(jù)的處理能力非常強,比如可以用它來檢測QQ號:

          1std::regex?qq_reg("[1-9]\\d{4,11}");
          2bool?ret?=?std::regex_match(qq,?qq_reg);
          3std::cout?<"valid"?:?"invalid")?<std::endl;

          是不是超級方便呢?那么接下來便來看看如何使用「正則表達式」。

          正則程序庫(regex)

          「正則表達式」就是一套表示規(guī)則的式子,專門用來處理各種復(fù)雜的操作。

          std::regex是C++用來表示「正則表達式」(regular expression)的庫,于C++11加入,它是class std::basic_regex<>針對char類型的一個特化,還有一個針對wchar_t類型的特化為std::wregex。

          正則文法(regex syntaxes)

          std::regex默認使用是ECMAScript文法,這種文法比較好用,且威力強大,常用符號的意義如下:

          符號意義
          ^匹配行的開頭
          $匹配行的結(jié)尾
          .匹配任意單個字符
          […]匹配[]中的任意一個字符
          (…)設(shè)定分組
          \轉(zhuǎn)義字符
          \d匹配數(shù)字[0-9]
          \D\d 取反
          \w匹配字母[a-z],數(shù)字,下劃線
          \W\w 取反
          \s匹配空格
          \S\s 取反
          +前面的元素重復(fù)1次或多次
          *前面的元素重復(fù)任意次
          ?前面的元素重復(fù)0次或1次
          {n}前面的元素重復(fù)n次
          {n,}前面的元素重復(fù)至少n次
          {n,m}前面的元素重復(fù)至少n次,至多m次
          |邏輯或

          上面列出的這些都是非常常用的符號,靠這些便足以解決絕大多數(shù)問題了。

          匹配(Match)

          字符串處理常用的一個操作是「匹配」,即字符串和規(guī)則恰好對應(yīng),而用于匹配的函數(shù)為std::regex_match(),它是個函數(shù)模板,我們直接來看例子:

           1std::regex?reg("<.*>.*");
          2bool?ret?=?std::regex_match("value",?reg);
          3assert(ret);
          4
          5ret?=?std::regex_match("value",?reg);
          6assert(!ret);
          7
          8std::regex?reg1("<(.*)>.*");
          9ret?=?std::regex_match("value",?reg1);
          10assert(ret);
          11
          12ret?=?std::regex_match("
          value
          "
          ,?std::regex("<(.*)>value"));
          13assert(ret);
          14
          15//?使用basic文法
          16std::regex?reg2("<\\(.*\\)>.*",?std::regex_constants::basic);
          17ret?=?std::regex_match("value",?reg2);
          18assert(ret);

          這個小例子使用regex_match()來匹配xml格式(或是html格式)的字符串,匹配成功則會返回true,意思非常簡單,若是不懂其中意思,可參照前面的文法部分。

          對于語句中出現(xiàn)\\,是因為\需要轉(zhuǎn)義,C++11以后支持原生字符,所以也可以這樣使用:

          1std::regex?reg1(R"(<(.*)>.*)");
          2auto?ret?=?std::regex_match("value",?reg1);
          3assert(ret);

          但C++03之前并不支持,所以使用時要需要留意。

          若是想得到匹配的結(jié)果,可以使用regex_match()的另一個重載形式:

           1std::cmatch?m;
          2auto?ret?=?std::regex_match("value",?m,?std::regex("<(.*)>(.*)"));
          3if?(ret)
          4{
          5????std::cout?<std::endl;
          6????std::cout?<std::endl;
          7????std::cout?<std::endl;
          8}
          9
          10std::cout?<"----------------"?<std::endl;
          11
          12//?遍歷匹配內(nèi)容
          13for?(auto?i?=?0;?i?14{
          15????//?兩種方式都可以
          16????std::cout?<"?"?<std::endl;
          17}
          18
          19std::cout?<"----------------"?<std::endl;
          20
          21//?使用迭代器遍歷
          22for?(auto?pos?=?m.begin();?pos?!=?m.end();?++pos)
          23{
          24????std::cout?<std::endl;
          25}

          輸出結(jié)果為:

           1value
          216
          30
          4----------------
          5value?value
          6xml?xml
          7value?value
          8xml?xml
          9----------------
          10value
          11xml
          12value
          13xml

          cmatch是class template std::match_result<>針對C字符的一個特化版本,若是string,便得用針對string的特化版本smatch。同時還支持其相應(yīng)的寬字符版本wcmatch和wsmatch。

          在regex_match()的第二個參數(shù)傳入match_result便可獲取匹配的結(jié)果,在例子中便將結(jié)果儲存到了cmatch中,而cmatch又提供了許多函數(shù)可以對這些結(jié)果進行操作,大多方法都和string的方法類似,所以使用起來比較容易。

          m[0]保存著匹配結(jié)果的所有字符,若想在匹配結(jié)果中保存有子串,則得在「正則表達式」中用()標出子串,所以這里多加了幾個括號:

          1std::regex("<(.*)>(.*)")

          這樣這些子串就會依次保存在m[0]的后面,即可通過m[1],m[2],…依次訪問到各個子串。

          搜索(Search)

          「搜索」與「匹配」非常相像,其對應(yīng)的函數(shù)為std::regex_search,也是個函數(shù)模板,用法和regex_match一樣,不同之處在于「搜索」只要字符串中有目標出現(xiàn)就會返回,而非完全「匹配」。

          還是以例子來看:

           1std::regex?reg("<(.*)>(.*)");
          2std::cmatch?m;
          3auto?ret?=?std::regex_search("123value456",?m,?reg);
          4if?(ret)
          5{
          6????for?(auto&?elem?:?m)
          7????????std::cout?<std::endl;
          8}
          9
          10std::cout?<"prefix:"?<std::endl;
          11std::cout?<"suffix:"?<std::endl;

          輸出為:

          1value
          2xml
          3value
          4xml
          5prefix:123
          6suffix:456

          這兒若換成regex_match匹配就會失敗,因為regex_match是完全匹配的,而此處字符串前后卻多加了幾個字符。

          對于「搜索」,在匹配結(jié)果中可以分別通過prefix和suffix來獲取前綴和后綴,前綴即是匹配內(nèi)容前面的內(nèi)容,后綴則是匹配內(nèi)容后面的內(nèi)容。

          那么若有多組符合條件的內(nèi)容又如何得到其全部信息呢?這里依舊通過一個小例子來看:

           1std::regex?reg("<(.*)>(.*)");
          2std::string?content("123value456centerhahahawindowthe?end");
          3std::smatch?m;
          4auto?pos?=?content.cbegin();
          5auto?end?=?content.cend();
          6for?(;?std::regex_search(pos,?end,?m,?reg);?pos?=?m.suffix().first)
          7{
          8????std::cout?<"----------------"?<std::endl;
          9????std::cout?<std::endl;
          10????std::cout?<1)?<std::endl;
          11????std::cout?<2)?<std::endl;
          12????std::cout?<3)?<std::endl;
          13}

          輸出結(jié)果為:

           1----------------
          2value
          3xml
          4value
          5xml
          6----------------
          7center
          8widget
          9center
          10widget
          11----------------
          12window
          13vertical
          14window
          15vertical

          此處使用了regex_search函數(shù)的另一個重載形式(regex_match函數(shù)亦有同樣的重載形式),實際上所有的子串對象都是從std::pair<>派生的,其first(即此處的prefix)即為第一個字符的位置,second(即此處的suffix)則為最末字符的下一個位置。

          一組查找完成后,便可從suffix處接著查找,這樣就能獲取到所有符合內(nèi)容的信息了。

          分詞(Tokenize)

          還有一種操作叫做「切割」,例如有一組數(shù)據(jù)保存著許多郵箱賬號,并以逗號分隔,那就可以指定以逗號為分割符來切割這些內(nèi)容,從而得到每個賬號。

          而在C++的正則中,把這種操作稱為Tokenize,用模板類regex_token_iterator<>提供分詞迭代器,依舊通過例子來看:

          1std::string?mail("[email protected],[email protected],[email protected],[email protected]");
          2std::regex?reg(",");
          3std::sregex_token_iterator?pos(mail.begin(),?mail.end(),?reg,?-1);
          4decltype(pos)?end;
          5for?(;?pos?!=?end;?++pos)
          6{
          7????std::cout?<str()?<std::endl;
          8}

          這樣,就能通過逗號分割得到所有的郵箱:

          1123@qq.vip.com
          2456@gmail.com
          3789@163.com
          4[email protected]

          sregex_token_iterator是針對string類型的特化,需要注意的是最后一個參數(shù),這個參數(shù)可以指定一系列整數(shù)值,用來表示你感興趣的內(nèi)容,此處的-1表示對于匹配的正則表達式之前的子序列感興趣;而若指定0,則表示對于匹配的正則表達式感興趣,這里就會得到“,";還可對正則表達式進行分組,之后便能輸入任意數(shù)字對應(yīng)指定的分組,大家可以動手試試。

          替換(Replace)

          最后一種操作稱為「替換」,即將正則表達式內(nèi)容替換為指定內(nèi)容,regex庫用模板函數(shù)std::regex_replace提供「替換」操作。

          現(xiàn)在,給定一個數(shù)據(jù)為"he…ll..o, worl..d!", 思考一下,如何去掉其中誤敲的“.”?

          有思路了嗎?來看看正則的解法:

          1char?data[]?=?"he...ll..o,?worl..d!";
          2std::regex?reg("\\.");
          3//?output:?hello,?world!
          4std::cout?<std::regex_replace(data,?reg,?"");

          我們還可以使用分組功能:

          1char?data[]?=?"001-Neo,002-Lucia";
          2std::regex?reg("(\\d+)-(\\w+)");
          3//?output:?001?name=Neo,002?name=Lucia
          4std::cout?<std::regex_replace(data,?reg,?"$1?name=$2");

          當(dāng)使用分組功能后,可以通過$N來得到分組內(nèi)容,這個功能挺有用的。

          實例(Examples)

          1. 驗證郵箱

          這個需求在注冊登錄時常有用到,用于檢測用戶輸入的合法性。

          若是對匹配精確度要求不高,那么可以這么寫:

          1std::string?data?=?"[email protected],[email protected],[email protected],[email protected]";
          2std::regex?reg("\\w+@\\w+(\\.\\w+)+");
          3
          4std::sregex_iterator?pos(data.cbegin(),?data.cend(),?reg);
          5decltype(pos)?end;
          6for?(;?pos?!=?end;?++pos)
          7{
          8????std::cout?<str()?<std::endl;
          9}

          這里使用了另外一種遍歷正則查找的方法,這種方法使用regex iterator來迭代,效率要比使用match高。這里的正則是一個弱匹配,但對于一般用戶的輸入來說沒有什么問題,關(guān)鍵是簡單,輸出為:

          1123@qq.vip.com
          2456@gmail.com
          3789@163.com
          4[email protected]

          但若我輸入一個“[email protected]”,它依舊能匹配成功,這明顯是個非法郵箱,更精確的正則應(yīng)該這樣寫:

           1std::string?data?=?"[email protected],?\
          2[email protected],?\
          3[email protected],?\
          4[email protected],?\
          5[email protected]?\
          6[email protected]"
          ;
          7std::regex?reg("[a-zA-z0-9_]+@[a-zA-z0-9]+(\\.[a-zA-z]+){1,3}");
          8
          9std::sregex_iterator?pos(data.cbegin(),?data.cend(),?reg);
          10decltype(pos)?end;
          11for?(;?pos?!=?end;?++pos)
          12{
          13????std::cout?<str()?<std::endl;
          14}

          輸出為:

          1123@qq.vip.com
          2456@gmail.com
          3789@163.com.cn.mail
          4[email protected]
          5haha@163.com.cn.com

          2. 匹配IP

          有這樣一串IP地址,192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.30,
          要求:取出其中的IP地址,并按地址段順序輸出IP地址。

          有點晚了,便不詳細解釋了,這里直接給出答案,可供大家參考:

           1std::string?ip("192.68.1.254?102.49.23.013?10.10.10.10?2.2.2.2?8.109.90.30");
          2
          3std::cout?<"原內(nèi)容為:\n"?<std::endl;
          4
          5//?1.?位數(shù)對齊
          6ip?=?std::regex_replace(ip,?std::regex("(\\d+)"),?"00$1");
          7
          8std::cout?<"位數(shù)對齊后為:\n"?<std::endl;
          9
          10//?2.?有0的去掉
          11ip?=?std::regex_replace(ip,?std::regex("0*(\\d{3})"),?"$1");
          12
          13std::cout?<"去掉0后為:\n"?<std::endl;
          14
          15//?3.?取出IP
          16std::regex?reg("\\s");
          17std::sregex_token_iterator?pos(ip.begin(),?ip.end(),?reg,?-1);
          18decltype(pos)?end;
          19
          20std::set<std::string>?ip_set;
          21for?(;?pos?!=?end;?++pos)
          22{
          23????ip_set.insert(pos->str());
          24}
          25
          26std::cout?<"------\n最終結(jié)果:\n";
          27
          28//?4.?輸出排序后的數(shù)組
          29for?(auto?elem?:?ip_set)
          30{
          31????//?5.?去掉多余的0
          32????std::cout?<std::regex_replace(elem,?
          33????????std::regex("0*(\\d+)"),?"$1")?<std::endl;
          34}

          輸出結(jié)果為:

           1原內(nèi)容為:
          2192.68.1.254?102.49.23.013?10.10.10.10?2.2.2.2?8.109.90.30
          3位數(shù)對齊后為:
          400192.0068.001.00254?00102.0049.0023.00013?0010.0010.0010.0010?002.002.002.002?008.00109.0090.0030
          5去掉0后為:
          6192.068.001.254?102.049.023.013?010.010.010.010?002.002.002.002?008.109.090.030
          7------
          8最終結(jié)果:
          92.2.2.2
          108.109.90.30
          1110.10.10.10
          12102.49.23.13
          13192.68.1.254

          THE END

          瀏覽 57
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  涩小说校园春色图片区视频区小说区 | 欧美一区二区三区四区视频 | 天天看搞欧美 | 欧美大黄视频 | 国产美女啪啪 |