-1
\$\begingroup\$

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

\$\endgroup\$
4
  • \$\begingroup\$ 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? \$\endgroup\$
    – MBaas
    Mar 9, 2018 at 8:56
  • \$\begingroup\$ There is this challenge, although not exactly a dupe. \$\endgroup\$
    – DELETE_ME
    Mar 9, 2018 at 9:05
  • \$\begingroup\$ 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". \$\endgroup\$
    – DELETE_ME
    Mar 9, 2018 at 11:11
  • 2
    \$\begingroup\$ @user202729 Right, I'm kind of awaiting it to be re-opened so I can dupe-hammer it. \$\endgroup\$
    – Adám
    Mar 9, 2018 at 11:20

9 Answers 9

3
\$\begingroup\$

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''.

\$\endgroup\$
2
  • \$\begingroup\$ Also works in Retina. \$\endgroup\$
    – Emigna
    Mar 9, 2018 at 11:49
  • \$\begingroup\$ @Emigna Thanks, added. \$\endgroup\$
    – Adám
    Mar 9, 2018 at 11:57
2
\$\begingroup\$

Python 2, 41 27 25 bytes

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 4 bytes

'/¡θ

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Java (OpenJDK 9), 25 bytes

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

Try it online!

Honorable mention

26 bytes:

s->("/"+s).split(".*/")[1]
\$\endgroup\$
1
\$\begingroup\$

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]

\$\endgroup\$
0
\$\begingroup\$

Stax, 4 bytes

'//H

Run and debug online!

Explanation

Split on "/", take last part.

\$\endgroup\$
0
\$\begingroup\$

Wolfram Language (Mathematica), 12 bytes

A built-in.

FileNameTake

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Haskell, 29 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Coconut, 19 bytes

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

Try it online!

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.