C++11 正則表達式——實例3


下面一個例子將進行年月日格式的轉換,將DD-MM-YYYY –> YYYY-MM-DD,其中‘.’或者‘/’都能正確識別。

 

#include <regex>

#include <iostream>

#include <string>

 

std::string format_date(const std::string& date)

{

   // regular expression

   const std:: regex pattern("(\\d{1,2})(\\.|-|/)(\\d{1,2})(\\.|-|/)(\\d{4})");

 

   // transformation pattern, reverses the position of all capture groups

   std::string replacer = "$5$4$3$2$1";

 

   // apply the tranformation

   return std:: regex_replace(date, pattern, replacer);

}

 

int main()

{

   std::string date1 = "1/2/2008";

   std::string date2 = "12.08.2008";

 

   std::cout << date1 << " -> " << format_date(date1) << std::endl;

   std::cout << date2 << " -> " << format_date(date2) << std::endl;

   std::cout << std::endl;

   return 0;

}

運行結果:

說明,這個例子也很有實用價值,這里用到的正則表達式的匹配模式前面都已經進行過說明就不在分析。

 

相信通過以上例子,對正則表達式的運用已經有了一個不錯的了解,下面再來添加一個實例,加深一下理解。

下面一個例子用來查找給定文本中new的個數和delete的個數是否相等:

#include <iostream>

#include <string>

#include <regex>

 

 

int main() {

// "new" and "delete" 出現的次數是否一樣?

    std::regex reg("(new)|(delete)");

    std::smatch m;

    std::string s=

    "Calls to new must be followed by delete. \

    Calling simply new results in a leak!";

    int new_counter=0;

    int delete_counter=0;

    std::string::const_iterator it=s.begin();

    std::string::const_iterator end=s.end();

   

    while (std::regex_search(it,end,m,reg))

    {

        // 是 new 還是 delete?

        m[1].matched ? ++new_counter : ++delete_counter;

        it=m[0].second;

    }

   

    if (new_counter!=delete_counter)

    std::cout << "Leak detected!\n";

    else

    std::cout << "Seems ok...\n";

    std::cout << std::endl;

}

運行結果:

運行結果表明,new和delete的數量不相等,也就是發生了“內存泄露”。

為了幫助理解,上面對於match_results類型的下標操作的意義,請看ISOIEC14882 C++11的說明:

#include <iostream>

#include <string>

#include <regex>

using namespace std;

 

class regex_callback {

  int sum_;

public:

  regex_callback() : sum_(0) {}

 

  template <typename T> void operator()(const T& what) {

    sum_+=atoi(what[1].str().c_str());

  }

 

  int sum() const {

    return sum_;

  }

};

 

int main() {

    regex reg("(\\d+),?");

    string s="1,1,2,3,5,8,13,21";

 

    sregex_iterator it(s.begin(),s.end(),reg);

    sregex_iterator end;

 

    regex_callback c;

    int sum=for_each(it,end,c).sum();//for_each返回的是這個函數對象,因此可以調用sum

   

    cout<<sum<<endl;

    cout<<endl;

}

運行結果:

#include <iostream>

#include <string>

#include <regex>

using namespace std;

int main()

{

        regex reg("/");

        vector<std::string> vec;

        string s="Split/Vulue/Teather/Neusoft/Write/By/Lanwei";

        sregex_token_iterator it(s.begin(),s.end(),reg,-1);//// -1逆向匹配,就是匹配除了'/'之外的

        sregex_token_iterator end ;

        while(it!=end)

            vec.push_back(*it++);

        copy(vec.begin(),vec.end(),ostream_iterator<std::string>( cout,"\n"));

}

運行結果:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM