どのようにC ++でのstd ::文字列のための基本的なpreg_match_allの交換を取得しますか?

StackOverflow https://stackoverflow.com/questions/2177731

  •  24-09-2019
  •  | 
  •  

質問

「基本」とは意味している:のみ演算子「+」( - >次...)と「|」 ( - >または)が必要とされている。

プロトタイプます:

preg_match_all(std::string pattern, std::string subject, std::vector<std::string> &matches)

使用例:

std::vector<std::string> matches;
std::string pattern, subject;
subject = "Some text with a lots of foo foo and " +  char(255) + " again " + char(255);
pattern = "/" + char(255) + char(255) + "+|foo+/";
preg_match_all(pattern, subject, matches);

マッチはmatches[n]を介して利用可能afterwardsaでなければなりません。誰かがを使用してのブーストの、および/またはPCREなしのヒントを得ましたか。そうでない場合、どのように私はこのブーストを実現しました!

役に立ちましたか?

解決

あなたはのstd ::文字列::検索を使って何かをロールアップでき、ファンクタを介してマッチを行うと、文字列ベクトルに結果を押します。

それはブーストで実現の道は、あなたが望むもののために行き過ぎおそらくある - 。あなたは最初lexemsへの発現を分解して、与えられた正規表現を解析するためのステートマシンをコンパイルする必要があります。

他のヒント

すべてのは、彼らがで発見されたインデックスと一致したの

これは、ベクトルを返します。

std::vector<std::pair<std::string, unsigned int>> RegexPP::MatchAll(std::string pattern, std::string haystack) {
    std::vector<std::pair<std::string, unsigned int>> matches;

    std::regex p(pattern);

    std::sregex_iterator start(haystack.begin(), haystack.end(), p), end;

    for(; start != end; start++) {
        auto match = *start; // dereference the iterator to get the match_result
        matches.push_back(std::pair<std::string, unsigned int>(match.str(), match.position()));
    }

    return matches;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top