Hi Bhagwat,

I try to accomplish that with sqlite3cpp (
https://github.com/yangacer/sqlite3cpp), a light wrapper of sqlite3 for C++.
Hope it help :-)


// Require sqlite3cpp to be installed from source.
// Compile with: clang++-3.6 -g sqlite_re.cpp -lsqlite3cpp -lsqlite3
-std=c++11
// ----
#include <regex>
#include <string>
#include <sstream>
#include <iostream>

#include "sqlite3cpp.h"


int main() {
    using namespace sqlite3cpp;

    database db(":memory:");

    // Register the lambda
    db.create_scalar("re_replace",
                     [](
                       std::string pattern,
                       std::string value,
                       std::string text)
                     {
                         // Replace regex |pattern| found in |text| with
|value|
                         std::stringstream out;
                         std::regex re(pattern);


 std::regex_replace(std::ostreambuf_iterator<char>(out),
                                            text.begin(), text.end(),
                                            re, value);
                         return out.str();
                     });

    // Test data
    auto csr = db.make_cursor();
    csr.executescript(
      "CREATE TABLE T (data TEXT);"
      "INSERT INTO T VALUES('Quick brown fox');"
      );

    // Replace vowels with '*'
    char const *query = "SELECT re_replace('a|e|i|o|u', '*', data) FROM T";

    // Execute the query and print out replaced results
    for(auto const &row : csr.execute(query)) {
        string_ref result;
        std::tie(result) = row.to<string_ref>();
        std::cout << result << std::endl;
    }

    // Should print:
    // Q**ck br*wn f*x

    return 0;
}


On Wed, May 4, 2016 at 8:25 PM, Bhagwat Balshetwar <
bhagwat.balshetwar at gmail.com> wrote:

> I want to write the custom function for regular expression using C/C++.
> How to write it. Is there the document/sample code available on this.
>
> -Bhagwat
> _______________________________________________
> sqlite-users mailing list
> sqlite-users at mailinglists.sqlite.org
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
>

Reply via email to