2012-04-24 74 views
0

在c#中有正則表達式,我可以用它來刪除一些任意字符或字符範圍,如Regex.Replace(str, "[^a-zA-Z0-9_.]+", "", RegexOptions.Compiled)。但是在C++中相當於什麼。我知道Boost有一個正則表達式庫。但是對於這個操作,它是否可行以及它的性能如何?什麼是從C++中的字符串中刪除字符的最好和最快捷的方式?相當於Regex.Replace in C++

+2

它的提升,你不能去那個錯了,我會說 – 2012-04-24 15:32:05

+2

如果你真的關心性能,使用[Boost.Xpressive(HTTP:// www.boost.org/libs/xpressive/)(特別是它的靜態正則表達式),而不是Boost.Regex。 – ildjarn 2012-04-24 16:47:42

回答

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); 
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)); 
} 

當然,如果你不需要正則表達式的力量,有大量的字符串函數,可能可以做的拼接字符更快的工作在一個字符串。