C + + Regular expression (regex) iterator (iterator) detailed

Source: Internet
Author: User
Tags expression include regular expression

The regular expression (regex), using Boost's regex header file, is the new standard for c++11, but gcc4.8.1 is not fully supported, so use boost library;

Specific installation: http://blog.csdn.net/caroline_wendy/article/details/17282187

The writing specification of regular expressions, taking ECMAScript as an example, using iterators can traverse the original string and output all the strings conforming to the requirements;

Using the prefix () and suffix () methods, you can output the previous unmatched string and the latter unmatched string;

A subexpression of a regular expression (subexpressions) that can be segmented out of a regular expression and decomposed in a regular expression with parentheses "()";

The code is as follows:

#include <iostream> #include <string> #include <algorithm> #include <boost/regex.hpp>  
using namespace Std;  
      
using namespace boost;  
    int main () {std::string pattern ("[^c]ei");  
    Pattern = ' [: alpha:]]* ' + pattern + ' [: alpha:]]* '; Boost::regex R (pattern, regex::icase);  
    Ignores case std::string str ("Ruby carolinei biubiubiu weindy spikei winnceiy");  
            Use the regular iterator for traversal for (Boost::sregex_iterator it (str.begin), Str.end (), R), End_it; It!=end_it;  
      
    ++it) std::cout << it->str () << Std::endl;  
    Outputs the front and back strings of regular expressions std::cout << Std::endl;  
            For (Boost::sregex_iterator It (Str.begin (), Str.end (), R), End_it; It!=end_it;  
        ++it) {Auto pos = It->prefix (). Length (); pos = pos>40?  
        pos-40:0; Std::cout << It->prefix (). STR (). substr (POS)//Output the previous unmatched string */<< "\n\t\t>>>" &LT;&L T it-&GT;STR () << "<<<\n" << it->suffix (). STR (). substr (0, 40)/* The string after output * *  
    <<std::endl;  
    ///Match subexpression std::string filename ("File.cqp MyGod.cpP"); Boost::regex Rsub ([[: alnum:]]+) \. (  
    CPP|CXX|CC) $ ", regex::icase);  
    Smatch results;  
if (boost::regex_search (filename, results, rsub)) Std::cout << results.str (1) << Std::endl; }

Output:

Carolinei  
weindy  
spikei  
      
Ruby   
        >>>Carolinei<<<  
 biubiubiu weindy Spikei Winnceiy  
 biubiubiu   
        >>>Weindy<<<  
 spikei winnceiy  
       
        >>>SpikeI<< <  
 Winnceiy  
Mygod

Author: csdn Blog spike_king

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

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.