2011-03-23 55 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);