Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I have data in that string is like f <- "./DAYA-1178/10TH FEB.xlsx". I would like to extract only DAYA-1178
f <- "./DAYA-1178/10TH FEB.xlsx"
DAYA-1178
what I have tried is
f1 <- gsub(".*./","", f)
But it is giving last result of my file "10TH FEB.xlsx"
"10TH FEB.xlsx"
Appreciate any lead.
stringi::stri_extract(f,regex = "(?<=\\./).*(?=/)")
It seems you are dealing with files. You need the basename of the directory:
basename(dirname(f)) [1] "DAYA-1178"
or you could do:
sub(".*/","",dirname(f)) [1] "DAYA-1178"
Add a comment
Using strsplit, we can split the input on path separator / and retain the second element:
strsplit
/
f <- "./DAYA-1178/10TH FEB.xlsx" unlist(strsplit(f, "/"))[2] [1] "DAYA-1178"
If you wish to use sub, here is one way:
sub
sub("^.*/(.*?)/.*$", "\\1", f) [1] "DAYA-1178"
f1 <- gsub("[.,xlsx]","",f)
u can try like these it will give
f1 <- /DAYA-1178/10TH FEB f3 <- strsplit(f1,"/")[[1]][2] DAYA-1178 --> answer
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
stringi::stri_extract(f,regex = "(?<=\\./).*(?=/)")