1

I have a list of strings:

c("brown::value-twostancenpq", "twitter_ld_homepage:xxxRestApisCreateTweet::create_longesttweetthereis::TaggedId",
"dumbRestApi::check_allProducts::ProductId", "dumbRestApi::check_allCustomers::CustomerId",
"twitter_ld_homepage:unknownApisCreateEdit::create_id::NewProfileId")

I am trying to extract the tail of the string that occurs after the last double-colons. Because of varying lengths and multiple occurrences of double-colons, I am finding it difficult to code this in R.

Desired Result
value-twostancenpq
TaggedId
ProductId
CustomerId
NewProfileId
Otto Kässi
  • 2,943
  • 1
  • 10
  • 27
user2845095
  • 465
  • 2
  • 9

2 Answers2

4

You can use sub with .*:: where .*:: matches everything until the last ::.

sub(".*::", "", s)
# sub(".*:", "", s) #Alternative for this case
#[1] "value-twostancenpq" "TaggedId"           "ProductId"         
#[4] "CustomerId"         "NewProfileId"      

Data

s <- c("brown::value-twostancenpq",
       "twitter_ld_homepage:xxxRestApisCreateTweet::create_longesttweetthereis::TaggedId",
       "dumbRestApi::check_allProducts::ProductId",
       "dumbRestApi::check_allCustomers::CustomerId",
       "twitter_ld_homepage:unknownApisCreateEdit::create_id::NewProfileId")
GKi
  • 37,245
  • 2
  • 26
  • 48
1

data:

mydata <-c("brown::value-twostancenpq", "twitter_ld_homepage:xxxRestApisCreateTweet::create_longesttweetthereis::TaggedId",
"dumbRestApi::check_allProducts::ProductId", "dumbRestApi::check_allCustomers::CustomerId",
"twitter_ld_homepage:unknownApisCreateEdit::create_id::NewProfileId")

You can use strsplit() to split each string into substrings separated by '::'.

As a result you get a list of vectors of varying lenghts:

> str_split(mydata, '::')
[[1]]
[1] "brown"              "value-twostancenpq"

[[2]]
[1] "twitter_ld_homepage:xxxRestApisCreateTweet"
[2] "create_longesttweetthereis"
[3] "TaggedId"

[[3]]
[1] "dumbRestApi"       "check_allProducts" "ProductId"

[[4]]
[1] "dumbRestApi"        "check_allCustomers" "CustomerId"

[[5]]
[1] "twitter_ld_homepage:unknownApisCreateEdit"
[2] "create_id"
[3] "NewProfileId"

You can use sapply to get the last element of each vector.

> sapply(strsplit(mydata, '::'), tail, 1)
[1] "value-twostancenpq" "TaggedId"           "ProductId"
[4] "CustomerId"         "NewProfileId"
Otto Kässi
  • 2,943
  • 1
  • 10
  • 27