10
\$\begingroup\$

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^!\[\]{}@$%+=]+$.

\$\endgroup\$
  • 17
    \$\begingroup\$ Please consider using the Sandbox in the future to get feedback on your challenges before posting them to the main site. \$\endgroup\$ – Mego May 16 '17 at 20:45
  • 1
    \$\begingroup\$ codegolf.meta.stackexchange.com/a/12432/59376 - Got this idea from your challenge. \$\endgroup\$ – Magic Octopus Urn May 16 '17 at 21:51
  • \$\begingroup\$ @carusocomputing nice challenge! \$\endgroup\$ – programmer5000 May 17 '17 at 0:06
  • 1
    \$\begingroup\$ why the negative votes? Is this challenge "exceedingly trivial" or so ? \$\endgroup\$ – Abel Tom May 17 '17 at 5:09
  • \$\begingroup\$ @AbelTom edit history would suggest downvotes were for the lack of specifications in the first draft of this question. \$\endgroup\$ – Patrick Roberts May 17 '17 at 5:12

86 Answers 86

1 2 3
1
\$\begingroup\$

><>, 50 bytes

i:0( ?v
v[:<2~<
r  ^]+1r<
>:"."=?v^
v?l<r~r<;
>o ^

Try it online!

\$\endgroup\$
1
\$\begingroup\$

CJam, 6 bytes

q'./W=

Try it online!

        Print
    W     the last
     =    element
   /      of the result of splitting
q         the input
 '.       on the character '.'
\$\endgroup\$
1
\$\begingroup\$

Add++, 9 bytes

L,"."$tbU

Try it online!

\$\endgroup\$
1
\$\begingroup\$

ActionScript 2.0, 42 bytes

function a(b){trace(b.split(".").pop());};

Technically the ;s aren't required for it to compile, at least in JPEXS, but it's good practice. Call:

a("a.b");

(traces "b")

\$\endgroup\$
  • \$\begingroup\$ Oh, OK. Looking at other examples, I didn't realise that. ActionScript doesn't really have a way to take inputs, so sure, delete this. Unless... \$\endgroup\$ – Jhynjhiruu Rekrap Mar 9 '18 at 20:28
  • \$\begingroup\$ Looks good. Happy golfing. \$\endgroup\$ – 0 ' Mar 9 '18 at 20:36
  • \$\begingroup\$ Awesome. By the way, putting the trace() inside the function is shorter when considering outputting, but then it requires a string be included when calling the function - is that allowed? \$\endgroup\$ – Jhynjhiruu Rekrap Mar 9 '18 at 20:39
  • \$\begingroup\$ I'm not quite sure what you mean by it requiring a string to be included when calling the function as I hardly know any Actionscript. In general it's allowed to mix and match any of the default allowed input and output methods. \$\endgroup\$ – 0 ' Mar 9 '18 at 20:54
  • \$\begingroup\$ function a(b){trace(b.split(".").pop());}; prints the output and is only 42 bytes, but calling the function would be a("a.b"); ActionScript has syntax very similar to JavaScript, so you can pretty much just read it like JavaScript. \$\endgroup\$ – Jhynjhiruu Rekrap Mar 9 '18 at 20:55
1
\$\begingroup\$

Aceto, 6 bytes

r'.:Qp

Try it online!

r       grabs input as string
 '.     literal period
   :    split string on period
    Q   grap bottom item
     p  print it
\$\endgroup\$
1
\$\begingroup\$

><>, 18 bytes

i:0(6$.:"."=?]
ro|

Try it online!

How It Works:

i:(6$.  Jump to the second line if out of input
      :"."=  Else check if the character is a .
           ?[ And create a new stack if it is
              Loop back to the beginning of the line
If it is end of input
ro|  Reverse the current stack once and output, erroring on the EOF (-1)
\$\endgroup\$
1
\$\begingroup\$

Stax, 4 bytes

'./H

Run and debug online!

Explanation

Split on ., take last part.

\$\endgroup\$
1
\$\begingroup\$

T-SQL, 48 bytes

SELECT RIGHT(F,CHARINDEX('.',REVERSE(F))) FROM T

SQL Fiddle

\$\endgroup\$
1
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 46 bytes

	I =INPUT
S	I '.' REM . I	:S(S)
	OUTPUT =I
END

Try it online!

Takes the input, then repeatedly replaces it with all the text following the first . until no .s remain, then outputs the value.

\$\endgroup\$
1
\$\begingroup\$

Funky, 26 bytes

s=>(k=s::split".")[(#k)-1]

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Jotlin, 17 bytes

split(".").last()

Full file:

data class Test(val input: String, val output: String)

val test = listOf(
        Test("hi.txt", "txt"),
        Test("carrot.meme", "meme"),
        Test("what..is..this..file", "file"),
        Test(".bashrc", "bashrc"),
        Test("T00M@n3KaPZ.h0wC[]h", "h0wC[]h"),
        Test("agent.000", "000")
)

fun String.f() = split(".").last()

for ((i, o) in test) {
    if (o != i.f()) {
        throw AssertionError()
    }
}
\$\endgroup\$
1
\$\begingroup\$

MBASIC, 129 bytes

1 INPUT F$:F=INSTR(F$,"."):IF F=0 THEN END
2 FOR I=LEN(F$) TO 1 STEP -1:P$=MID$(F$,I,1):O$=P$+O$:IF P$="." THEN PRINT O$:END
3 NEXT

Explanation

Get a filename. If it doesn't contain a period, bail out. Otherwise, collect letters from right to left to build an output string. When we see a period, print the string.

Output

? hi.txt
.txt

? .bashrc
.bashrc

? T00M@n3KaPZ.h0wC[]h
.h0wC[]h
\$\endgroup\$
1
\$\begingroup\$

R, 40 26 bytes

-14 bytes thanks to J.Doe

sub("^.*[.]","",scan(,""))

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 26 bytes \$\endgroup\$ – J.Doe Oct 27 '18 at 9:16
1
\$\begingroup\$

Red 9 bytes

Suffix? f

Assumes file is is in word 'f

\$\endgroup\$
  • 2
    \$\begingroup\$ Do you have a link to documentation or interpreters for that language? \$\endgroup\$ – Nissa Oct 29 '18 at 20:35
  • \$\begingroup\$ I don't think this does what the challenge asks. The challenge is asking for a program that takes the file name and gives the extension. \$\endgroup\$ – Post Rock Garf Hunter Oct 29 '18 at 20:54
1
\$\begingroup\$

Pepe, 48 bytes

rEeeEeEEEeREEeREEEerEEREEEEEEEreererEEEEeEeereee

Try it online!

\$\endgroup\$
1
\$\begingroup\$

C (gcc), 77 60 52 bytes

char*f(char*s){for(s+=strlen(s);*--s-46;);return s;}

Try it online!

-17 bytes from Jonathan Frech

-8 bytes by removing i and doing arithmetic on s directly

Ungolfed (same strategy):

char *extension(char *original) {
    original = original + strlen(original);
    while(original[0] != '.') --original;
    return original;
}
\$\endgroup\$
1
\$\begingroup\$

Turing Machine But Way Worse, 1315 1287 bytes

0 0 0 1 1 0 0
1 0 1 1 8 0 0
0 1 0 1 2 0 0
1 1 1 1 9 0 0
0 2 0 1 3 0 0
1 2 1 1 a 0 0
0 3 0 1 4 0 0
1 3 1 1 b 0 0
0 4 0 1 5 0 0
1 4 1 1 c 0 0
0 5 0 1 6 0 0
1 5 1 1 d 0 0
0 6 0 1 7 0 0
1 6 1 1 e 0 0
0 7 0 1 f 0 0
1 7 1 1 0 0 0
0 8 0 1 9 0 0
1 8 1 1 9 0 0
0 9 0 1 a 0 0
1 9 1 1 a 0 0
0 a 0 1 b 0 0
1 a 1 1 b 0 0
0 b 0 1 c 0 0
1 b 1 1 c 0 0
0 c 0 1 d 0 0
1 c 1 1 d 0 0
0 d 0 1 e 0 0
1 d 1 1 e 0 0
0 e 0 1 0 0 0
1 e 1 1 0 0 0
0 f 0 0 g 0 0
1 f 1 0 g 0 0
0 g 0 0 h 0 0
1 g 1 0 p 0 0
0 h 0 0 q 0 0
1 h 1 0 i 0 0
0 i 0 0 r 0 0
1 i 1 0 j 0 0
0 j 0 0 s 0 0
1 j 1 0 k 0 0
0 k 0 0 l 0 0
1 k 1 0 t 0 0
0 l 0 0 u 0 0
1 l 1 0 m 0 0
0 m 0 0 n 0 0
1 m 1 0 v 0 0
0 n 0 0 o 0 0
1 n 1 0 g 0 0
0 p 0 0 q 0 0
1 p 1 0 q 0 0
0 q 0 0 r 0 0
1 q 1 0 r 0 0
0 r 0 0 s 0 0
1 r 1 0 s 0 0
0 s 0 0 t 0 0
1 s 1 0 t 0 0
0 t 0 0 u 0 0
1 t 1 0 u 0 0
0 u 0 0 f 0 0
1 u 1 0 f 0 0
0 o 0 1 w 0 0
1 o 1 1 w 0 0
0 w 0 1 x 0 0
1 w 1 1 E 0 0
0 x 0 1 y 0 0
1 x 1 1 F 0 0
0 y 0 1 z 0 0
1 y 1 1 G 0 0
0 z 0 1 A 0 0
1 z 1 1 H 0 0
0 A 0 1 B 0 0
1 A 1 1 I 0 0
0 B 0 1 C 0 0
1 B 1 1 J 0 0
0 C 0 1 D 0 0
1 C 1 1 K 0 0
0 D 0 1 D 0 1
1 D 1 1 w 1 0
0 E 0 1 F 0 0
1 E 1 1 F 0 0
0 F 0 1 G 0 0
1 F 1 1 G 0 0
0 G 0 1 H 0 0
1 G 1 1 H 0 0
0 H 0 1 I 0 0
1 H 1 1 I 0 0
0 I 0 1 J 0 0
1 I 1 1 J 0 0
0 J 0 1 K 0 0
1 J 1 1 K 0 0
0 K 0 1 w 1 0
1 K 1 1 w 1 0

Try it online!

Wow, this is big.

\$\endgroup\$
  • \$\begingroup\$ Wow this is incredible! \$\endgroup\$ – MilkyWay90 Aug 10 '19 at 5:47
0
\$\begingroup\$

Emacs, 6 bytes

The cursor needs to be at the start of the line containing the string.

This will delete the entire line if there's no extension.

C-<SPC> C-e C-r . <RET> <BACKSPACE>

Explanation:

C-<SPC>      start a selection
C-e          go to the end of the line
C-r . <RET>  search for "." backward
<BACKSPACE>  delete the selected text (which should be the text before the last ".")
\$\endgroup\$
0
\$\begingroup\$

Pushy, 13 bytes

K46-$v;F@46+"

Try it online!

               \ Implicit: string on stack as character codes
K46-           \ Subtract 46 from each code point, mapping '.' to 0
    $v;        \ While the top of stack is non-zero, move to auxiliary stack
       F       \ Copy auxiliary stack onto main stack
        @      \ Reverse (to obtain original order)
         46+   \ Add 46 (to obtain original characters)
            "  \ Print as a string.  
\$\endgroup\$
0
\$\begingroup\$

Brain-Flak, 76 bytes

{((((([()()()]){}){}){}()){}{}<>)<>}<>{({}(((()()()()()){}()){}()){}<>)<>}<>

Try it online!

Explanation:

{          loop over stack
  (                            push
    (((([()()()]){}){}){}()){} -46
  {}                           plus the top of the stack
  <>)                          to the other side
<>}        retuen for more

<>      on the other side...

{       loop until 0
  (                             push
     {}                         the top of the stack
     (((()()()()()){}()){}()){} +46
  <>)                           to the first side
<>}       return for more

<> print the first side

6 bytes less than the other Brain-Flack solution.

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

Perl 6, 11 bytes

{m/\.\w+$/}

Try it online!

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

Befunge-98 (FBBI), 50 bytes

{v
 >~:a`!#v_
--2*86:$<:u-10_v#
02-u0}>:#,_@   >00

Try it online!

Puts string onto stack, then iterates through it in reverse, putting each new character onto new stack. Once a . is encountered, discard original stack and output new stack in 0gnirts format.

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

Forth (gforth), 45 bytes

: f begin 1 -1 d+ s" ."search 0= until type ;

Try it online!

Code Explanation

: f              \ start a new word definition
  begin          \ start an indefinite loop
    1 -1 d+      \ remove the first character from the string
    s" ."search  \ find the length and starting address of the first substring that starts with '.'
    0=           \ check if '.' was found in the string
  until          \ end the loop if not
  type           \ output the result
;                \ end the word definition
\$\endgroup\$
0
\$\begingroup\$

33 v1.0, 13 bytes

Gets the file name passed as an argument and prints the extension (without leading '.') to standard output.

1bt'.'ywmcbtp

Explanation:

1b            (Gets the second item in argv, the filename input)
  t'.'y       (Splits that by '.')
       wmcb   (Gets the last element in the list)
           tp (Prints to standard output)
\$\endgroup\$
0
\$\begingroup\$

brainfuck, 45 bytes

,[>,]>+[[-]++[<->------]<---<[->+>+<<]>]>[.>]

Try it online!

\$\endgroup\$
-1
\$\begingroup\$

Python & JavaScript, 22 bytes

'x.h'.split('.').pop()

Explanation

I did split the string 'x.h', which is the full file name at the dot and then I did remove the last index in the list (the extension) and this happens to return the value of the removed list item which in my case would be the extension.

Note: This could run on either JavaScript or Python without adaptations

\$\endgroup\$
  • 4
    \$\begingroup\$ Welcome to the site! A couple things: rather than hardcoding the x.h, you need to take it as input, as a parameter, or through another allowed input method. Also, this answer requires a read-evaluate-print loop (REPL) in order to work; this must be specified. \$\endgroup\$ – Khuldraeseth na'Barya Mar 13 '18 at 0:38
  • 1
    \$\begingroup\$ Welcome to the site! Unfortunately, this is only a snippet, and so is invalid. You can make it into a lambda like this: lambda x:x.split('.').pop(), or Python REPL like this: input().split('.').pop() (input format depends on whether you use Python 2 or Python 3). \$\endgroup\$ – Erik the Outgolfer Mar 13 '18 at 12:05
1 2 3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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