How do I extract text from a URL path in R?

bayesian.asian

I have a dataframe, with a column with entries in the format of:

/textIwant
/textIwant/otherstuff
/

I want to create a new column extracting the "textIwant". Should I be using strsplit, or regex?

akrun

We can use str_extract to extract one or more characters that are not a /

library(stringr)
str_extract(str1,  "[^/]+")
#[1] "textIwant"   "textIwant"   "abc-def-ghi" "abc-def-ghi"

Or with sub from base R to match the characters that are not a /, capture it as a group (([^/]+)) and replace with the backreference (\\1)

sub("^.([^/]+).*", "\\1", str1)
#[1] "textIwant"   "textIwant"   "abc-def-ghi" "abc-def-ghi"

data

str1 <- c("/textIwant", "/textIwant/otherstuff", "/abc-def-ghi/", "/abc-def-ghi")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I extract the folder path from file path in Python?

How do I extract text between two characters in R

How do I extract raw data from plot element in R

How do I extract the number before a text with variable spacing in r?

How do I extract the path before the "/" in R for multiple list.files?

How do I extract text from a file to a variable while also replacing that text with a marker?

How do I extract text from awk results?

How do I extract only the url from this element?

How do I extract each words from a text file in scala

How do I extract data from a response to a callback URL?

How do I extract an impl AsRef<Path> value from a Box<dyn AsRef<Path>>?

How do I extract specific parts of text from a string in R?

How do I extract quoted portions from a text in perl?

Nginx Server: How do I remove the folder path from the URL?

How do I extract parameters from listed models in R?

How do I extract words from a list in a text in R?

How do I extract text at even positions?

How to extract url from text with ASP?

How do I extract a path from html with sed?

Extract file extension from URL path in R

How do I extract text and numbers from a string within a cell?

How can I remove "/url?q=" from text data in R

How to extract path from Windows shortcut in R?

How do i extract text from email body using UiPath?

How to extract relative path from a URL in postgresql

How do I extract only number from dynamic headline/text?

How do I extract value from a text file in Makefile?

How do I extract a text from a website(html)

How do I extract elements of a martix from a list in r?