Extract filename from path

-1

Given a printable ASCII string representing a path to a file on a POSIX-compliant system, return just the filename, i.e. remove everything until and including the last slash (/):

/full/path/file.name.extfile.name.ext

nopath.herenopath.here

dir/.hidden.hidden

/dirs.can/have.exts/filefile

./herehere

don't\do thisdon't\do this

MBaas

Posted 2018-03-09T08:46:56.153

Reputation: 115

Question was closed 2018-03-09T12:30:42.123

I‘m just curious to see how this is done in a variety of languages...ie whether there‘s any jumping through hoops or if is built-in. Can you suggest a better way to find that out? – MBaas – 2018-03-09T08:56:34.037

There is this challenge, although not exactly a dupe.

– user202729 – 2018-03-09T09:05:02.300

Now, I doubt that there is any difference between this and the "what's the file extension" challenge, as they're just "shortest substring after a <'.' or '/', depends on the challenge". – user202729 – 2018-03-09T11:11:58.787

2@user202729 Right, I'm kind of awaiting it to be re-opened so I can dupe-hammer it. – Adám – 2018-03-09T11:20:53.823

Answers

3

QuadR, 4 bytes

Retina, 4 bytes

.*/

Try QuadR online!

Try Retina online!

Replace as many characters as possible, followed by a slash, with nothing.

Equivalent to the 9-byte Dyalog APL function '.*/'⎕R''.

Adám

Posted 2018-03-09T08:46:56.153

Reputation: 37 779

Also works in Retina. – Emigna – 2018-03-09T11:49:50.967

@Emigna Thanks, added. – Adám – 2018-03-09T11:57:14.520

2

Python 2, 41 27 25 bytes

lambda s:s.split('/')[-1]

Try it online!

TFeld

Posted 2018-03-09T08:46:56.153

Reputation: 19 246

2

05AB1E, 4 bytes

'/¡θ

Try it online!

Emigna

Posted 2018-03-09T08:46:56.153

Reputation: 50 798

2

Java (OpenJDK 9), 25 bytes

s->s.replaceAll(".*/","")

Try it online!

Honorable mention

26 bytes:

s->("/"+s).split(".*/")[1]

Olivier Grégoire

Posted 2018-03-09T08:46:56.153

Reputation: 10 647

1

Red, 24 bytes

func[s][last split s"/"]

Try it online!

Red has a built-in split-path but it needs a path agrument and not a string.

f: func[p][last split-path to-file p]

Galen Ivanov

Posted 2018-03-09T08:46:56.153

Reputation: 13 815

0

Stax, 4 bytes

'//H

Run and debug online!

Explanation

Split on "/", take last part.

Weijun Zhou

Posted 2018-03-09T08:46:56.153

Reputation: 3 396

0

Wolfram Language (Mathematica), 12 bytes

A built-in.

FileNameTake

Try it online!

alephalpha

Posted 2018-03-09T08:46:56.153

Reputation: 23 988

0

Haskell, 29 bytes

r.fst.span(/='/').r
r=reverse

Try it online!

totallyhuman

Posted 2018-03-09T08:46:56.153

Reputation: 15 378

0

Coconut, 19 bytes

n->n.split('/')[-1]

Try it online!

ovs

Posted 2018-03-09T08:46:56.153

Reputation: 21 408