2011-03-23 65 views
1

我想弄清楚我可以在C++(linux)中使用的preg_replace()(php)樣式函數。linux c/C++ preg_replace類型的函數?

有人可以幫我翻譯這個嗎?

$str = preg_replace(array('/\s+/','/[^A-Za-z0-9\-]/'),array('-',''),$str); 
+0

我刪除的C代碼,因爲你是在寫C++。 – Puppy 2011-03-23 19:54:23

回答

3

最好的辦法是與Perl Compatible Regular Expression(PCRE)庫鏈接,並使用它提供的功能。

您可以檢查pcrecpp(3)獲取更多信息。示例代碼將是:

#include <pcrecpp.h> 

pcrecpp::RE("\s+").Replace("-", &s); // where s is the target string 
pcrecpp::RE("[^A-Za-z0-9\-]").Replace("", &s);