为什么gsub在Ubuntu上表现不同?

时间:2018-04-05 11:49:22

标签: r regex gsub

我无法在所有Linux发行版上进行测试,但在使用Ubuntu时,gsub的行为似乎有所不同:

url <- "http://thinkr.fr/"
gsub("(.*)/", "\\1", url) 

#Returns

# on mac, Windows, CentOS
[1] "http://thinkr.fr" 

# On Ubuntu (plain and Mint) 
[1] "http:/thinkr.fr" 

Ubuntu返回http:/,其他所有人都返回http://

知道为什么吗?

以下是产生第二个输出的Linux Mint的会话信息:

> sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 18.2

Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.6.0
LAPACK: /usr/lib/lapack/liblapack.so.3.6.0

locale:
 [1] LC_CTYPE=fr_FR.UTF-8       LC_NUMERIC=C               LC_TIME=fr_FR.UTF-8       
 [4] LC_COLLATE=fr_FR.UTF-8     LC_MONETARY=fr_FR.UTF-8    LC_MESSAGES=fr_FR.UTF-8   
 [7] LC_PAPER=fr_FR.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] stringr_1.3.0 tidyr_0.8.0   dplyr_0.7.4   purrr_0.2.4   glue_1.2.0    attempt_0.2.0
[7] jsonlite_1.5  httr_1.3.1   

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.16     assertthat_0.2.0 R6_2.2.2         magrittr_1.5     pillar_1.2.1    
 [6] stringi_1.1.7    rlang_0.2.0      curl_3.0         neo4r_0.0.0.9000 bindrcpp_0.2    
[11] tools_3.4.3      igraph_1.2.1     yaml_2.1.17      compiler_3.4.3   pkgconfig_2.0.1 
[16] tidyselect_0.2.4 bindr_0.1        tibble_1.4.2

enter image description here

> extSoftVersion()
                               zlib                               bzlib 
                            "1.2.8"                "1.0.6, 6-Sept-2010" 
                                 xz                                PCRE 
                       "5.1.0alpha"                   "8.38 2015-11-23" 
                                ICU                                 TRE 
                                 ""           "TRE 0.8.0 R_fixes (BSD)" 
                              iconv                            readline 
                       "glibc 2.23"                               "6.3" 
                               BLAS 
"/usr/lib/libblas/libblas.so.3.6.0" 
> pcre_config()
             UTF-8 Unicode properties                JIT              stack 
              TRUE               TRUE               TRUE               TRUE 

1 个答案:

答案 0 :(得分:2)

在您上传的图片中显示您的问题,您的字符串末尾没有“/”,因此它会捕获到最后一个“/”,它只是http://

中的第二个斜杠

所以问题是你使用了不同的输入。