<div dir="ltr"><div><div>Hi All: I was trying to do something with regular expressions in Rcpp so I piggy backed<br></div>heavily off of Dirk's boost.regex example in the Rcpp Gallery where he takes streams of digits and checks them for machine and human readability. <br><br>My problem is actually pretty different and simpler. Essentially, if a character string ends in "rhofixed" or "norhofixed", then that part of the character string should be removed. The R code below illiustrates what I'm trying to do. <br><br>But, when I write the Rcpp code to do the same thing and set Sys.setenv("PKG_LIBS"="-lboost_regex"), I don't get the same result. I don't know if it's due to the regex engine being different in boost or I could be doing something else wrong. Thanks for any help.<br></div><br>===================================================================== <br><div># R CODE TO ILLUSTRATE WHAT I WANT<br></div><div>===================================================================== <br><br>s <- c("lngimbintrhofixed","lngimbnointnorhofixed","test") <br>result <- sub("(no)?rhofixed$","",s)<br>print(result)<br><br> print(result)<br>[1] "lngimbint" "lngimbnoint" "test" <br><br>#===================================================================== <br></div><div>Rcpp CODE ATTEMPT HEAVILY BASED OFF DIRK"S EXAMPLE <br></div><div>#===================================================================== <br><br>#include <Rcpp.h><br>#include <string><br>#include <boost/regex.hpp><br><br>using namespace Rcpp;<br>using namespace std;<br><br>bool validate_modelstring(const std::string& s) {<br> static const boost::regex e("^(.*)((no)?rhofixed)$");<br> return(boost::regex_match(s, e));<br>}<br><br>const std::string model_format("\\1");<br>const boost::regex e("\\A^(.*)((no)?rhofixed)$\\z");<br><br>std::string filtered_rhopart(const std::string& s) {<br> return boost::regex_replace(s, e, model_format, boost::match_default | boost::format_sed);<br>}<br><br>// [[Rcpp::export]]<br>Rcpp::DataFrame regexTest(std::vector<std::string> s) {<br> <br> int n = s.size(); <br> std::vector<bool> valid(n);<br> std::vector<string> outmodel(n);<br> <br> for (int i=0; i<n; i++) {<br> valid[i] = validate_modelstring(s[i]);<br> if (valid[i]) {<br> outmodel[i] = filtered_rhopart(s[i]);<br> } else {<br> outmodel[i] = s[i];<br> } <br> }<br><br> return Rcpp::DataFrame::create(Rcpp::Named("input") = s,<br> Rcpp::Named("valid") = valid,<br> Rcpp::Named("output") = outmodel);<br>}<br><br>/*** R<br>s <- c("lngimbintrhofixed","lngimbnointnorhofixed","test")<br>regexTest(s)<br>*/<br><br><br><br><br><br><br><br><br><br></div></div>