-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
  • 355,584
  • 18
  • 123
  • 178
1

We can do

sub('.*/([^/.]+).*', "\\1", x)
#[1] "baishan
akrun
  • 789,025
  • 32
  • 460
  • 575