0

If I have a character vector:

 links <- c("http://fdsfdsfdsfsdaaa.com/t5/this/bd-p/fdsfsdfdsfscshdad/dasd",
            "http://ffdsfdddddfdf.com/t5/that/bd-p/fdsfdsfsddfjfsd")

I want to extract "this" and "that" knowing that they are between "t5" and "bd-p." Totally lost on this one.

0

2 Answers 2

4

Using sub:

sub(".*t5/(.*)/bd-p.*","\\1",links)
[1] "this" "that"
Sign up to request clarification or add additional context in comments.

1 Comment

Really awesome! Thank you agstudy and anyone else that tried. I love this community so much.
1

Try this:

lapply(regmatches(links, regexec("t5/(.*)/bd-p", links)), '[', 2)
[[1]]
[1] "this"

[[2]]
[1] "that"

regexec combined with regmatches is good for getting subexpressions (i.e. the stuff in the parentheses). regmatches will return the whole search string and the subexpression, which is why I extract only the second element, which is the subexpression.

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.