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>>>" <&L T it->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/