[C/C ++ 11] _ [primary] _ [use regular expression library regex]

Source: Internet
Author: User

[C/C ++ 11] _ [primary] _ [use regular expression library regex]
The scenario regular expression is easy to replace when dealing with a very large number of string searches. If the string matches a little complex, it cannot be done without a regular expression. C ++ 11 provides us with a regular expression library. it is easier to use than boost's regular expression library. it must be funny to engage in Java. This is a standard feature of Java. How can C ++ 11 support this library and vs2010 support it. we recommend that you use regular expressions to replace string search with less code and speed. it indicates the regular expression syntax. If the pattern string is written incorrectly in vs2010, the running will crash. generally, an error is reported when the regular expression method is called. This is basically because the mode string is wrong. there are a lot of regular expressions, grouping forward and backward... Let's take a look at the reference. Generally, it is enough to learn it all. Example

<Code class = "language-html hljs"> // 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>  </p> "" <ol> <li> "" </li> <li> ddd Chinese Song </li> <li>" "</li> <li> xxx </li> <li>" </li> </ol> "" <p>  </p>" "< ol> <li> "" </li> <li> xxxxx </li> <li> "</li> </ol>" "<p>  </p> "; void TestReplace () {std: cout <"TestReplace ===" <std: endl; // Replace the absolute paths of all img src with the relative paths starting with images. // use the group. std: reg Ex img_regex ("(] * 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; // search for all the complete img tags std: regex img_regex ("] +>"); // use std: regex_search to query the first matched string. std: smatch colo R_match; std: cout <"regex_search =" <std: endl; if (std: regex_search (kHtmlSnippet, color_match, img_regex) {std :: cout <color_match [0] <'\ n';} // use the std: regex_iterator class to perform 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_iator Tor 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 ;}</iostream> </assert. h> </string> </regex> </code>

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% "height =" 30% ">   TestReplace ===< p>  </p> <ol> <li> ddd Chinese Song </li> <li> xxx </li> </ol> <p>  </p> <ol> <li> xxxxx </li> </ ol> <p>  </p>

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.