1

読みやすくするために、無視される空白/改行で std::regex 文字列をフォーマットできますか? Python VERBOSEのように利用可能なオプションはありますか?

詳細なし:

charref = re.compile("&#(0[0-7]+"
                     "|[0-9]+"
                     "|x[0-9a-fA-F]+);")

冗長な場合:

charref = re.compile(r"""
 &[#]                # Start of a numeric entity reference
 (
     0[0-7]+         # Octal form
   | [0-9]+          # Decimal form
   | x[0-9a-fA-F]+   # Hexadecimal form
 )
 ;                   # Trailing semicolon
""", re.VERBOSE)
4

2 に答える 2

8

文字列を複数のリテラルに分割し、次のように C++ コメントを使用するだけです。

std::regex rgx( 
   "&[#]"                // Start of a numeric entity reference
   "("
     "0[0-7]+"           // Octal form
     "|[0-9]+"           // Decimal form
     "|x[0-9a-fA-F]+"    // Hexadecimal form
   ")"
   ";"                   // Trailing semicolon
);

"&[#](0[0-7]+|[0-9]+|x[0-9a-fA-F]+);"その後、それらはコンパイラによって結合されます。これにより、無視されない正規表現に空白を追加することもできます。ただし、追加の引用符により、これを書くのが少し面倒になる可能性があります。

于 2016-06-10T14:40:30.687 に答える
5
inline std::string remove_ws(std::string in) {
  in.erase(std::remove_if(in.begin(), in.end(), std::isspace), in.end());
  return in;
}

inline std::string operator""_nows(const char* str, std::size_t length) {
  return remove_ws({str, str+length});
}

現在、これは をサポートしていません# commentsが、追加は簡単なはずです。文字列からそれらを取り除く関数を作成するだけで、次のようになります。

std::string remove_comments(std::string const& s)
{
  std::regex comment_re("#[^\n]*\n");
  return std::regex_replace(s, comment_re, "");
}
// above remove_comments not tested, but you get the idea

std::string operator""_verbose(const char* str, std::size_t length) {
  return remove_ws( remove_comments( {str, str+length} ) );
}

完了すると、次のようになります。

charref = re.compile(R"---(
 &[#]                # Start of a numeric entity reference
 (
     0[0-7]+         # Octal form
   | [0-9]+          # Decimal form
   | x[0-9a-fA-F]+   # Hexadecimal form
 )
 ;                   # Trailing semicolon
)---"_verbose);

そして完了。

于 2016-06-10T14:41:26.717 に答える