私は自分のアプリケーションの読み込み手順を書いています。これには、ファイルからのデータの読み取りと、適切なプロパティを持つ適切なオブジェクトの作成が含まれます。
このファイルは、次の形式の連続したエントリ (改行で区切られている) で構成されています。
=== OBJECT TYPE ===
<Property 1>: Value1
<Property 2>: Value2
=== END OBJECT TYPE ===
多くの場合、値は任意の文字、改行などで構成される文字列です。
std::regex
この形式に一致する を作成し、std::regex_iterator
各オブジェクトを順番にファイルに読み込むために使用できるようにしたいと考えています。
ただし、このタイプの形式に一致する正規表現を作成するのに問題があります。ECMAScript 構文を確認し、次の方法で正規表現を作成しましたが、テスト アプリケーションの文字列と一致しません。
const std::regex regexTest( "=== ([^=]+) ===\\n([.\\n]*)\\n=== END \\1 ===" );
これを次のテスト アプリケーションで使用すると、正規表現と文字列の照合に失敗します。
int main()
{
std::string testString = "=== TEST ===\n<Random Example>:This is a =test=\n<Another Example>:Another Test||\n=== END TEST ===";
std::cout << testString << std::endl;
const std::regex regexTest( "=== ([^=]+) ===\\n([.\\n]*)\\n=== END \\1 ===" );
std::smatch regexMatch;
if( std::regex_match( testString, regexMatch, regexTest ) )
{
std::cout << "Prefix: \"" << regexMatch[1] << "\"" << std::endl;
std::cout << "Main Body: \"" << regexMatch[2] << "\"" << std::endl;
}
return 0;
}