[Rcpp-devel] regular expression in Rcpp

Matt D. matdzb at gmail.com
Tue Jan 13 15:41:57 CET 2015


On 1/13/2015 07:27, Mark Leeds wrote:
> Hi All: I was trying to do something with regular expressions in Rcpp 
> so I piggy backed
> 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.
Hi!
>
> 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.
>
> 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.
It does indeed seems simpler than an usual use case for regex. Hence, 
perhaps you can go about it differently:
- use `boost::algorithm::ends_with` (from 
<boost/algorithm/string/predicate.hpp>) to check whether your predicate 
(i.e., a character string ends in "rhofixed" or "norhofixed") holds
- if so, use the `erase` member function 
(http://en.cppreference.com/w/cpp/string/basic_string/substr) to get rid 
of the substring (note: if one of the aforementioned predicates is 
satisfied, then you can trivially infer how long is the substring 
subject to removal -- and can thus use, say, the iterator-pair version, 
stepping backward from `std::end(your_string)` as needed).

HTH!

Best,

Matt



More information about the Rcpp-devel mailing list