2

My data:

d1 <- "GET /api/dir/100%ABC/user/prints/abcd1111/file"
d2 <- "GET /api/dir/100%ABC/prints/efgh1111/file"

I want to extract the string:user/prints or prints part from my data. I know how to extract between 2 certain strings like:

str_extract(d1, "(?<=/dir/)(.*)(?=%ABC)")

to get the number 100.

How to extract after %ABC/ and end with prints(contains prints) so I can get :user/prints or prints from same script.

3 Answers 3

2

In base R, we can use sub to extract everything after "%ABC" till "prints".

sub(".*%ABC/(.*prints).*", "\\1", d1)
#[1] "user/prints"

sub(".*%ABC/(.*prints).*", "\\1", d2)
#[1] "prints"
Sign up to request clarification or add additional context in comments.

Comments

2

Not exactly elegant, but this should do:

paste0(str_extract(d1, "(?<=%ABC)(.*)(?=prints)"),"prints")
# [1] "/user/prints"
paste0(str_extract(d2, "(?<=%ABC)(.*)(?=prints)"),"prints")
# [1] "/prints"

Comments

1

We can use gsub from base R

gsub(".*ABC/|/\\w+/\\w+$", "", d1)
#[1] "user/prints"
gsub(".*ABC/|/\\w+/\\w+$", "", d2)
#[1] "prints"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.