-1
x <- 'D:/r4ds/map.json/baishan.json'

I just wanna get "baishan".How can I realize it?

2 Answers2

2

You use basename + file_path_sans_ext from tools.

x <- 'D:/r4ds/map.json/baishan.json'
tools::file_path_sans_ext(basename(x))
#[1] "baishan"

Using pure regex :

sub('.*/(.*)\\..*', '\\1', x)

This extracts everything after last "/" till the following dot (".").

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

We can do

sub('.*/([^/.]+).*', "\\1", x)
#[1] "baishan
akrun
  • 874,273
  • 37
  • 540
  • 662