简体   繁体   English

R中的beg2char函数(qdap包)

[英]beg2char function in R (qdap package)

I am trying keep only that part of the string left of "keyword" . 我正在尝试只保留"keyword"留下的字符串部分。 Anything on the right of "keyword" should be removed. 应删除"keyword"右侧的任何内容。 beg2char seems like the best choice but its not doing what I thought it would do. beg2char似乎是最好的选择,但它没有做我认为它会做的事情。

Please advise: 请指教:

x <-"/index.php/front/yellow/searchHeading/heading/926/h_name/Architects/keyword/A//"

beg2char(x,"keyword")
# [1] "/in"

We could use, gsub as below: 我们可以使用gsub如下:

gsub("keyword.*", "", x)
# [1] "/index.php/front/yellow/searchHeading/heading/926/h_name/Architects/"

If we want to keep the "keyword" in the output, then set include = TRUE : 如果我们想在输出中保留"keyword" ,则设置include = TRUE

library(qdap)

x <-"/index.php/front/yellow/searchHeading/heading/926/h_name/Architects/keyword/A//"

beg2char(x, "keyword", include = TRUE)
# [1] "/index.php/front/yellow/searchHeading/heading/926/h_name/Architects/keyword"

If we want to exclude "keyword" , then we would do as you did, which doesn't work, because letter "d" is part of the "keyword" . 如果我们想要排除"keyword" ,那么我们会像你一样做,这不起作用,因为字母"d""keyword" Looks like a bug to me, submitted an issue at GitHub:qdap . 看起来像是一个bug,在GitHub上提交了一个问题:qdap

But this works: 但这有效:

beg2char(x, "k")
# [1] "/index.php/front/yellow/searchHeading/heading/926/h_name/Architects/"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM