1

I am trying keep only that part of the string left of "keyword". Anything on the right of "keyword" should be removed. beg2char seems like the best choice but its not doing what I thought it would do.

Please advise:

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

beg2char(x,"keyword")
# [1] "/in"
zx8754
  • 52,746
  • 12
  • 114
  • 209
nrcjea001
  • 1,027
  • 1
  • 9
  • 21

2 Answers2

2

We could use, gsub as below:

gsub("keyword.*", "", x)
# [1] "/index.php/front/yellow/searchHeading/heading/926/h_name/Architects/"
zx8754
  • 52,746
  • 12
  • 114
  • 209
Mist
  • 1,888
  • 1
  • 14
  • 21
  • I think they wan't solution using qdap package. – zx8754 Jul 27 '18 at 06:01
  • Why would you use a package rather than core R if they are both as simple as each other? Not trying to be smart, I'm seriously interested to know. – Mist Jul 27 '18 at 06:06
  • No idea, it is just my guess. qdap is in the title, post is tagged with qdap, OP is trying to use qdap function. Nothing against your solution. – zx8754 Jul 27 '18 at 06:09
1

If we want to keep the "keyword" in the output, then set 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". Looks like a bug to me, submitted an issue at GitHub:qdap.

But this works:

beg2char(x, "k")
# [1] "/index.php/front/yellow/searchHeading/heading/926/h_name/Architects/"
zx8754
  • 52,746
  • 12
  • 114
  • 209