3

I have data in that string is like f <- "./DAYA-1178/10TH FEB.xlsx". I would like to extract only DAYA-1178

what I have tried is

f1 <- gsub(".*./","", f) 

But it is giving last result of my file "10TH FEB.xlsx"

Appreciate any lead.

1
  • 1
    Try stringi::stri_extract(f,regex = "(?<=\\./).*(?=/)") Commented Mar 3, 2020 at 5:45

3 Answers 3

4

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"
Sign up to request clarification or add additional context in comments.

Comments

1

Using strsplit, we can split the input on path separator / and retain the second element:

f <- "./DAYA-1178/10TH FEB.xlsx"
unlist(strsplit(f, "/"))[2]
[1] "DAYA-1178"

If you wish to use sub, here is one way:

sub("^.*/(.*?)/.*$", "\\1", f)
[1] "DAYA-1178"

Comments

0
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

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.