顯示具有 pcre 標籤的文章。 顯示所有文章
顯示具有 pcre 標籤的文章。 顯示所有文章

2018年10月20日 星期六

[SQL] 在 SQLite3 環境中,使用 Regular Expression @ Ubuntu 16.04

$ sudo apt-get install sqlite3-pcre
$ file /usr/lib/sqlite3/pcre.so
/usr/lib/sqlite3/pcre.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=0afc1236e89d4a8b99746f231049101172714c2c, stripped
$ sqlite3
SQLite version 3.11.0 2016-02-15 17:29:24
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> SELECT date();
2018-10-19
sqlite> SELECT date() AS d WHERE d REGEXP '[0-9]';
Error: no such function: REGEXP
sqlite> .load /usr/lib/sqlite3/pcre.so
sqlite> SELECT date() AS d WHERE d REGEXP '[0-9]';
2018-10-19
sqlite> SELECT date() AS d WHERE d REGEXP '^[0-9]$';
sqlite> SELECT date() AS d WHERE d REGEXP '^[0-9]+$';
sqlite> SELECT date() AS d WHERE d REGEXP '^[0-9\-]+$';
2018-10-19

2014年9月10日 星期三

[C++] 使用 PCRE、RE2 進行 Match all (如同 PHP preg_match_all 效果) @ Ubuntu 14.04

對 PHP 來說:

$ vim t.php
<?php
$data = "..<a href='...'>...</a>.."; //file_get_contents(...);
if (preg_match_all("#<a[^h]*href=['\"]{0,1}([^\"']+)[\"']{0,1}[^>]*>(.*?)</a>#", $data, $matches) )
        print_r($matches);
$ php t.php
Array
(
    [0] => Array
        (
            [0] => <a href='...'>...</a>
        )

    [1] => Array
        (
            [0] => ...
        )

    [2] => Array
        (
            [0] => ...
        )

)


對 PCRE 來說:

$ vim pcre_test.cpp
#include <pcre.h>
#include <iostream>

int main() {

const char *error;
int erroroffset;
pcre *preg_pattern_a_tag = pcre_compile("<a[^h]*href=['\"]{0,1}([^\"']+)[\"']{0,1}[^>]*>(.*?)</a>", PCRE_MULTILINE, &error,  &erroroffset, NULL);

if (!preg_pattern_a_tag) {
std::cout << "ERROR\n";
return -1;
}

std::string raw = "..<a href='...'>...</a>..";

unsigned int offset = 0;
unsigned int len = raw.size();
int matchInfo[3*2] = {0};
int rc = 0;

while (offset < len && (rc = pcre_exec(preg_pattern_a_tag, 0, raw.c_str(), len, offset, 0,  matchInfo, sizeof(matchInfo))) >= 0) {
for (int n=0; n<rc ; ++n) {
int data_length = matchInfo[2*n+1] - matchInfo[2*n];
std::cout << "Found:[" << raw.substr(matchInfo[2*n], data_length) << "]\n";
}
offset = matchInfo[1];
}
return 0;
}
$ g++ -std=c++11 pcre_test.cpp -lpcre
$ ./a.out
Found:[<a href='...'>...</a>]
Found:[...]
Found:[...]


對 RE2 來說:

$ vim re2_test.cpp
#include <re2/re2.h>
#include <iostream>

int main() {

//RE2 preg_pattern_a_tag("<a[^h]*href=['\"]{0,1}([^\"']+)[\"']{0,1}[^>]*>(.*?)</a>", RE2::Latin1);
RE2 preg_pattern_a_tag("<a[^h]*href=['\"]{0,1}([^\"']+)[\"']{0,1}[^>]*>(.*?)</a>");

std::string raw = "..<a href='...'>...</a>..";

re2::StringPiece result_a_href, result_a_body;

while(RE2::PartialMatch(raw, preg_pattern_a_tag, &result_a_href, &result_a_body)) {
std::cout << "result_a_href:[" << result_a_href << "]\n";
std::cout << "result_a_body:[" << result_a_body << "]\n";
raw = result_a_body.data();
}
return 0;
}
$ g++ -std=c++11 re2_test.cpp /path/libre2.a -lpthread
$ ./a.out
result_a_href:[...]
result_a_body:[...]

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