Is there a table somewhere on the internet that maps common perl regular expressions into equivalent C++11 code? Basically, I would like a table that maps common Perl Regular Expressions such as 's///' and s///m' and '//i' into equivalent code using std::string and C++11 Regular Expressions and presents this information in a side by side comparison table. Example:
$line = "one two three four four SIX"; $line =~ s/two/six/; $line =~ s/four/ten/g; $line =~ s/th(re)e/whe$1/ if ($line =~ /six/i) { }
How do I write each of these Perl expressions in C++11 code? I'll get things started below. (I challenge people to make this table more complete and to post it somewhere on the internet. One further challenge beyond this question is to make the exact same comparison for C#, Java and other languages to Perl regular expressions.)
//================================================ // C++11 Perl equivalency wrapper functions //================================================ #include <string> #include <regex> #include <iostream> #include <vector> using namespace std; // Equivalent to Perl: $s =~ s/$e1/$e2/g; inline void StrReplaceXg(string& s, string e1, string e2) { string r = regex_replace(s, regex{e1}, e2); s = r; } // Equivalent to Perl: $s =~ s/$e1/$e2/; inline void StrReplaceX(string& s, string e1, string e2) { string r = regex_replace(s, regex{e1}, e2, regex_constants::format_first_only); s = r; } // Equivalent to Perl: if ($s =~ /$e1/) { /*dosomething with m[0]...*/ + } inline bool StrMatchX(string& s, string e1, vector<string>& m) { smatch M; m.clear(); bool rc = regex_match(s, M, regex{e1}); if (rc) { for(int i=1; i < (int) M.size(); i++) { m.push_back(M[i].str()); } } return rc; } //================================================ // Example 1: C++11 vs Perl //================================================ // PERL: //sub example1() { // $s = "one two three four four"; // $s =~ s/two/stuff/; // print "s:$s\n"; //} // C++11: void example1() { string s = string{"one two three four four"}; StrReplaceX (s, R"(two)", "stuff"); cout << "s:" << s << "\n"; } //================================================ // Example 2: C++11 vs Perl //================================================ // PERL: //sub example2() { // $s = "one two three four four"; // $s =~ s/four/stuff/g; // print "s:$s\n"; //} // C++11: void example2() { string s = string{"one two three four four"}; StrReplaceXg (s, R"(four)", "stuff"); cout << "s:" << s << "\n"; } //================================================ // Example 3: C++11 vs Perl //================================================ // PERL: //sub example3() { // $s = "one two three four four"; // if ($s =~ /(\S+) (\S+)/) { // print "match1: $1\n"; // print "match2: $2\n"; // } //} // C++11: void example3() { string s = string{"one two three four four"}; vector<string> m; if (StrMatchX(s, R"((\S+) (\S+))", m)) { cout << "match1:" << m[0] << "\n"; cout << "match2:" << m[1] << "\n"; } } int main(int, char**) { example1(); example2(); example3(); return 0; }

In reply to How to rewrite perl pattern subtitution 's///' into C++11 Regular Expression? by anonuser2342

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.