Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

different output using stringi and gsub (using the same pattern on the same string)

Tags:

r

gsub

stringi

I wish to know why I obtain two different output strings by using gsub and stringi. Does the metacharacter "." not include new lines in stringi? Does stringi read "line by line"?

By the way I did not find any way to perform the "correct" substitution with stringi so I needed to use gsub here.

string <- "is it normal?\n\nhttp://www.20minutes.fr"

> gsub(" .*?http"," http", string)
[1] "is http://www.20minutes.fr"

> stri_replace_all_regex(string, " .*?http"," http")
[1] "is it normal?\n\nhttp://www.20minutes.fr"
like image 851
Dario Lacan Avatar asked Sep 28 '22 07:09

Dario Lacan


1 Answers

One way would be to set . to also match line terminators instead of stopping at a line:

stri_replace_all_regex(string, " .*?http"," http", 
                       opts_regex = stri_opts_regex(dotall = TRUE))
like image 82
lukeA Avatar answered Oct 03 '22 00:10

lukeA