[c/c++11]_[primary]_[using regular expression library regex]

Source: Internet
Author: User

Scene
    1. Regular expressions are very easy to handle when dealing with very small string lookups, and if you match a string that is slightly more complex, there is no regular expression to do it.
    2. C++11 provides us with a library of regular expressions. It is easier to use than the boost regular library.
    3. Make Java must feel very funny, this is the standard function of Java, how c++11 only support this library, vs2010 to support. It is recommended to use regular bar when dealing with string search and replace, the code quantity is small, fast.
Reference

Std::regex_replace
Std::regex_iterator
Regular Expressions (c + +)

Description
    1. Syntax for regular expressions I'm not going to say much, vs2010. If the pattern string is incorrectly written, the run will also crash. General method of calling regular methods times wrong is basically the pattern string is wrong.
    2. The regular syntax is really a lot, forward backwards, grouped ... See the reference bar, generally do not need to learn all the enough.
Example
Test_reg.cpp: Defines the entry point of the console application. #include "stdafx.h" #include<regex>#include<string>#include<assert.h>#include<iostream>static std::string Khtmlsnippet = "<p><img src=\ "d:\\doc\\& #x4E2A;& #x4EBA;& #x8F6F;& #x4EF6;\\& #x9700; & #x6C42;& #x6587;& #x6863;\\& #x5B89;& #x5353;& #x52A9;& #x624B; \\android\\images\\main\ \main.png\ " width=\" 30%\ " height=\" 30%\ "></P>""<ol>""<li>DDD Chinese Songs</li>""<li>Xxx</li>""</ol>""<p><img src=\ "d:\\doc\\& #x4E2A;& #x4EBA;& #x8F6F;& #x4EF6;\\& #x9700; & #x6C42;& #x6587;& #x6863;\\& #x5B89;& #x5353;& #x52A9;& #x624B; \\android\\images\\main\ \main-about.png\ " width=\" 30%\ " height=\" 30%\ "></P>""<ol>""<li>Xxxxx</li>""</ol>""<p><img src=\ "d:\\doc\\& #x4E2A;& #x4EBA;& #x8F6F;& #x4EF6;\\& #x9700; & #x6C42;& #x6587;& #x6863;\\& #x5B89;& #x5353;& #x52A9;& #x624B; \\android\\images\\main\ \main-setting.png\ " width=\" 30%\ " height=\" 30%\ "></P>"; void Testreplace () {std::cout<< "testreplace == = = = = =" << Std::endl;    Replace the absolute path of all img src with the relative path starting with images . You can use grouping . Std::regex Img_regex (<img [^>    ]*src=[\ "']{1}" ([^\ "']*" \\\\ (images\\\\[^\ "']*[\" ']{1}[^>]*>) ");    Std::smatch Color_match;    Std::string rep = "$1$3";    std::string tmp = Std::regex_replace (KHTMLSNIPPET,IMG_REGEX,REP); Std::cout<< tmp << Std::endl;} void Testsearch    () { std::cout << "testsearch = == = = = = = =" << Std::endl; Find All img full tags std::regex img_regex("<img [^>]+> ");    Use Std::regex_search to query the first matching string.    Std::smatch Color_match; Std::cout<< "Regex_search===="<<Std::endl;if(Std::regex_search(Khtmlsnippet,Color_match,Img_regex))    {Std::cout<<Color_match[0] << ' \N‘; }//Use classStd::regex_iteratorTo do multiple searches..    Std::cout<< "Sregex_iterator===="<<Std::endl;Auto Words_begin=Std::sregex_iterator(Khtmlsnippet.begin(),Khtmlsnippet.end(),Img_regex);Auto Words_end=Std::sregex_iterator(); for(Std::sregex_iterator I=Words_begin;I!=Words_end; ++I)    {Std::smatch Match= *I;std::string Match_str=Match.str();Std::cout<<Match_str<< ' \N‘; }}int _tmain(int argc,_tchar*argv[]){Testsearch();Testreplace();return 0;}

Output:

Testsearch ====regex_search = = = =<img src="d:\doc\& #x4E2A;& #x4EBA;& #x8F6F;& #x4EF6;\& #x9700; & #x6C42;& #x6587;& #x6863;\& #x5B89;& #x5353;& #x52A9;& #x624B; \android\images\main\ Main.png " width=" 30% " height=" 30% ">Sregex_iterator = = =<img  src  = "d:\doc\& #x4E2A;& #x4EBA;& #x8F6F;& #x4EF6;\& #x9700;& #x6C42; x6587;& #x6863;\& #x5B89;& #x5353;& #x52A9;& #x624B; \android\images\main\main.png " width  = "30%"  he  ight  = "30%" ; <img  src  = "d:\doc\& #x4E2A;& #x4EBA;& #x8F6F;& #x4EF6;\& #x9700;& #x6C42; x6587;& #x6863;\& #x5B89;& #x5353;& #x52A9;& #x624B; \android\images\main\main-about.png " width  = "30%"   Height  = "30%" ; <img src="d:\doc\& #x4E2A;& #x4EBA;& #x8F6F;& #x4EF6;\& #x9700; & #x6C42;& #x6587;& #x6863;\& #x5B89;& #x5353;& #x52A9;& #x624B; \android\images\main\ Main-setting.png " width="" height="30%">Testreplace = = =<p><img src="Images\main\main.png" width="30%"  Height="30%"></P><ol><li>DDD Chinese Songs</li><li>Xxx</li></ol><p><img src="Images\main\main-about.png" width="30%" Hei  ght="30%"></P><ol><li>Xxxxx</li></ol><p><img src="Images\main\main-setting.png"width="30%"  Height="30%"></P>

[c/c++11]_[primary]_[using regular expression library regex]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.