Regex 如何将perl模式替换的///重写为C++11正则表达式?

Regex 如何将perl模式替换的///重写为C++11正则表达式?,regex,c++11,Regex,C++11,互联网上有没有一个表可以将常见的perl正则表达式映射成等价的C++11代码 基本上,我想要一个表,该表使用std::string和C++11正则表达式将常见的Perl正则表达式(如“s///”和“s///m”和“///I”)映射为等效代码,并在并排比较表中显示此信息 例如: $line = "one two three four four SIX"; $line =~ s/two/six/; $line =~ s/four/ten/g; $line =~ s/th(re)e/wh

互联网上有没有一个表可以将常见的perl正则表达式映射成等价的C++11代码

基本上,我想要一个表,该表使用std::string和C++11正则表达式将常见的Perl正则表达式(如“s///”和“s///m”和“///I”)映射为等效代码,并在并排比较表中显示此信息

例如:

$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) {

}

如何在C++11代码中编写这些Perl表达式?

我将从下面开始。我向人们挑战,让这个表格更加完整,并将其发布在互联网上的某个地方。超越这个问题的另一个挑战是将C、Java和其他语言与Perl正则表达式进行完全相同的比较

//================================================
// 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, const string& e1, const string& e2) {
  string r = regex_replace(s, regex{e1}, e2);
  s = r;
}

// Equivalent to Perl: $s =~ s/$e1/$e2/;
inline void StrReplaceX(string& s, const string& e1, const string& e2) {
  string r = regex_replace(s, regex{e1}, e2,
                regex_constants::format_first_only);
  s = r;
}

// Case-Sensitive Match
// Equivalent to Perl: if ($s =~ /$e1/) { /*dosomething with m[0]...*/ }
inline bool StrSearchX(const string& s, const string& e1, vector<string>& m) {
   smatch  M;
   m.clear();
   bool rc = regex_search(s, M, regex{e1});
   if (rc) {
       for(int i=1; i < (int) M.size(); i++) {
          m.push_back(M[i].str());
       }
   }
   return rc;
}

// Case Insensitive Match
// Equivalent to Perl: if ($s =~ /$e1/i) { /*dosomething with m[0]...*/ }
inline bool StrSearchXi(const string& s, const string& e1, vector<string>& m) {
   smatch  M;
   m.clear();
   bool rc = regex_search(s, M, regex{e1, regex::icase});
   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 (StrSearchX(s, R"((\S+) (\S+))", m)) {
    cout << "match1:" << m[0] << "\n";
    cout << "match2:" << m[1] << "\n";
  }
}


int main(int, char**) {
  example1();
  example2();
  example3();
  return 0;
}

C++和Perl的工作方式不同,但共享一种正则表达式语言。要真正使用它们,还有更多的工作要做。例如,您的第一个替换对象如下所示:

#include <iostream>
#include <regex>
#include <string>

int main() {
  std::string line = "one two three four four SIX";
  std::regex two("two");
  line = std::regex_replace(line, two, "six");
  std::cout << line << "\n";
  return 0;
}

使用一个简单的字符串替换的正则表达式,在C++中绝对是过度的,但是你可以看到它是如何工作的。