# Extract filename from path [duplicate]

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

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

# Retina, 4 bytes

.*/



Try Retina online!

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

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

• Also works in Retina. Mar 9, 2018 at 11:49
Mar 9, 2018 at 11:57

# Python 2, 4127 25 bytes

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


Try it online!

# 05AB1E, 4 bytes

'/¡θ


Try it online!

# Java (OpenJDK 9), 25 bytes

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


Try it online!

### Honorable mention

26 bytes:

s->("/"+s).split(".*/")[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]

# Stax, 4 bytes

'//H


Run and debug online!

## Explanation

Split on "/", take last part.

# Wolfram Language (Mathematica), 12 bytes

A built-in.

FileNameTake


Try it online!

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

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