Regular Expression Regular Expression-4 (C ++)

xiaoxiao2021-03-06  68

Because I always feel that the C code on the upper reply is too footed, my heart is unwilling. After all, C is a very good language, and there are many excellent template libraries. This simple applet is used by me, it is really too embarrassing. The code is absolutely should not be so bloated. In fact, I have a few concepts can't be blurred. Therefore, the bloated code is bloated, and the concept of input and output flow is blurred, and one is the regular expression application is not familiar. The input output stream is then revoked, and the documentation of the BOOST library of the regular expression is read in detail. The new code is as follows, which is significantly more elegant than the original code:

#include #include #include #include #include using namespace std; using namespace boost; string filter (const string in) {regex expr ( "/ "(// w ): (// w )% (// w ) /" "); String FMT (" $ 1: $ 2 * $ 3, / N "); ostringstream ostring; ostream_iterator Oi (ostring); Regex_replace (OI, IN.BEGIN (), In.end (), EXPR, FMT, (Match_Default | format_no_copy); Return Ostring.Str ();} int main (int Argc, const char * argv []) {ix (Argc <3) {cout << "please enter 2 filenames (eg in.txt out.txt) << endl; return 1;} ifstream in (Argv [1]); OFSTREAM OUT (Argv [2]); Ostringstream Buf; buf << in.rdbuf (); out << filter (buf.str ()) << flush;}

转载请注明原文地址:https://www.9cbs.com/read-110541.html

New Post(0)