0

のような任意の文字または文字範囲を削除するために使用できる c# の正規表現がありますRegex.Replace(str, "[^a-zA-Z0-9_.]+", "", RegexOptions.Compiled)。しかし、C++ でこれに相当するものは何ですか。Boostには正規表現ライブラリがあることは知っています。しかし、この操作は実行可能で、パフォーマンスはどれくらいですか? c++ で文字列から文字を削除する最良かつ迅速な方法は何ですか?

4

2 に答える 2

0

おそらくboost::regex_replaceが必要です:

#include <boost/regex.hpp>
#include <string>

const std::string input; 
boost::regex matcher("[^a-zA-Z0-9_.]+");
const std::string formatter("");

std::string output = boost::regex_replace(input, matcher, formatter);
于 2012-04-24T16:58:20.400 に答える
0

Boost を使ってみたところ、すばやく簡単に使用できることがわかりました。例:

#include <boost/regex.hpp>

bool detect_mypattern( const string& text )
{
    // A specific regex pattern
    static const boost::regex ep("[\\w\\s]{8}\\s{1}\\w{2}\\s{1}Test");
    return( boost::regex_match(text, ep) );
}

もちろん、正規表現の力が必要ない場合は、文字列から文字をつなぎ合わせる作業をより高速に実行できる文字列関数がたくさんあります。

于 2012-04-24T15:38:40.620 に答える