anonuser2342 has asked for the wisdom of the Perl Monks concerning the following question:

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; }

Replies are listed 'Best First'.
Re: How to rewrite perl pattern subtitution 's///' into C++11 Regular Expression?
by Laurent_R (Canon) on Jan 15, 2017 at 22:47 UTC
      Your links mention "ECMA-Script syntax" for the regexes.

      So a search for differences between JS and Perl should help.

      IIRC does JS Script mostly implement the Perl 4 syntax, so some things will be missing.

      (With little differences like not supporting an /s modifier and requiring something like [^] top match all including newline. )

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

Re: How to rewrite perl pattern subtitution 's///' into C++11 Regular Expression?
by LanX (Saint) on Jan 16, 2017 at 00:49 UTC
Re: How to rewrite perl pattern subtitution 's///' into C++11 Regular Expression?
by Anonymous Monk on Jan 15, 2017 at 22:42 UTC
    Why reimplement the wheel? http://pcre.org, perlembed...
      NB the name "pcre" is a misleading one, it's not 100% compatible.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        Yes, but any DIY solution like the OP wants will be way less compatible :-)
Re: How to rewrite perl pattern subtitution 's///' into C++11 Regular Expression?
by LanX (Saint) on Jan 16, 2017 at 13:00 UTC