2014年7月7日 星期一

[Linux] 使用 PCRE 進行字串取代 (C++/CPP/Regular Express/Replace) @ Ubuntu 14.04

安裝:

$ sudo apt-get install libpcre3-dev

程式碼:

#include <iostream>
#include <string>
#include <pcrecpp.h>

int main() {
        std::string s = "Hello 123-234-456 World";
        pcrecpp::RE("[0-9]+").GlobalReplace("", &s);
        std::cout << "Result: " << s <<"\n";
        return 0;
}


結果:

$ g++ -g t.cpp -lpcrecpp && ./a.out
Result: Hello -- World

1 則留言:

  1. I recommend RE2, written by author of Go lang.
    Official repo: https://code.google.com/p/re2/ .

    And, "If you absolutely need backreferences and generalized assertions, then RE2 is not for you, but you might be interested in irregexp, Google Chrome's regular expression engine", quote from above link.

    回覆刪除