# What's the file extension?

Your challenge is to find the file extension of a provided filename:

hi.txt -> txt or .txt
carrot.meme -> meme or .meme
lol (undefined behavior)
what..is..this..file -> file or .file
.bashrc -> bashrc or .bashrc
T00M@n3KaPZ.h0wC[]h -> h0wC[]h or .h0wC[]h
agent.000 -> 000 or .000

You must get the text from the last . or after the last . to the end of the string. The first capturing group match of the regular expression /\.([^.]+)$/ works, and so does splitting the input on .s and returning the last one. The file name will always contain at least one ., but it may contain multiple .. (see examples) The input will always match ^[.a-zA-Z0-9^!{}@$%+=]+$. • Please consider using the Sandbox in the future to get feedback on your challenges before posting them to the main site. – user45941 May 16 '17 at 20:45 • codegolf.meta.stackexchange.com/a/12432/59376 - Got this idea from your challenge. – Magic Octopus Urn May 16 '17 at 21:51 • @carusocomputing nice challenge! – programmer5000 May 17 '17 at 0:06 • why the negative votes? Is this challenge "exceedingly trivial" or so ? – Abel Tom May 17 '17 at 5:09 • @AbelTom edit history would suggest downvotes were for the lack of specifications in the first draft of this question. – Patrick Roberts May 17 '17 at 5:12 ## 92 Answers # Octave, 24 bytes @(x)strsplit(x,'.'){end} Creates an anonymous function named ans which can accept a string as input Online Demo # Pyth, 5 bytes ecz\. try it here # Charcoal, 6 4 bytes -2 bytes thanks to Erik the Outgolfer ⊟⪪Ｓ. Try it online! ## Explanation ⊟ Pop ⪪ . Split on "." Ｓ Next input as string Implicit print of value • Can't you use ⊟x instead of §x±¹? – Erik the Outgolfer May 17 '17 at 13:13 # REXX 53 parse value reverse(arg(1)) with e "." say reverse(e) # C#, 35 bytes s=>s.Substring(s.LastIndexOf(".")); Try it online! ## Haskell, 3230 29 bytes -1 byte thanks to Laikoni r=reverse r.fst.span(/='.').r self-explanatory • takeWhile can be shortened to fst.span. – Laikoni May 17 '17 at 16:17 # Cheddar, 16 bytes @.sub(/.*\./,"") simple regex solution. Alternative solution using split: @.split('.')[-1] # JS (ES6), 26 25 bytes x=>/\.([^.]+)$/.exec(x)[1]

-1 byte thanks to Shaggy

document.querySelector('pre').innerText = (x=>/\.([^.]+)$/.exec(x)[1])("example.txt") <input oninput = "document.querySelector('pre').innerText = (x=>x.match(/\.([^.]+)$/)[1])(this.value)" value = "example.txt">
<pre></pre>

Alternate, 29 bytes:

x=>(y=x.split.)[y.length-1]

Try it online!

# Julia, 22 bytes

g(n)=split(n,'.')[end]

->s{s[/[^.]+\$/]}

Try it online!