21
\$\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\$
7
  • 19
    \$\begingroup\$ Please consider using the Sandbox in the future to get feedback on your challenges before posting them to the main site. \$\endgroup\$
    – user45941
    May 16 '17 at 20:45
  • 1
    \$\begingroup\$ codegolf.meta.stackexchange.com/a/12432/59376 - Got this idea from your challenge. \$\endgroup\$ May 16 '17 at 21:51
  • \$\begingroup\$ @carusocomputing nice challenge! \$\endgroup\$
    – user58826
    May 17 '17 at 0:06
  • 2
    \$\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\$ May 17 '17 at 5:12

96 Answers 96

1
\$\begingroup\$

Ohm v2, 5 bytes

I..ï⁾

Try it online!

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

Perl 5, 8 bytes

7 bytes code + 1 for -p.

s;.*\.;

Try it online!

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

Excel, 58 bytes

=TRIM(RIGHT(SUBSTITUTE(A1,".",REPT(" ",LEN(A1))),LEN(A1)))
\$\endgroup\$
1
\$\begingroup\$

Excel VBA, 29 bytes

An anonymous VBE immediate window function that takes input via cell [A1] and outputs to the console. Return string is of the form .Extension.

?Mid([A1],InStrRev([A1],"."))
\$\endgroup\$
1
\$\begingroup\$

Yabasic, 34 bytes

A answer

Takes input as a string from STDIN, and outputs of the form .Extension to STDOUT.

Input""s$
?Mid$(s$,RInStr(s$,"."))

Try it online!

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

Google Sheets, 25 bytes

An anonymous worksheet function that takes input from cell A1 and outputs the detected file extension of the form Extension to the calling cell.

=RegexExtract(A1,"[^\.]+$
\$\endgroup\$
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\$
7
  • \$\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\$ 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\$ 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\$ 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\$

C#, 47 34 27 bytes

System.IO.Path.GetExtension

Saved 6 bytes thanks to @totallyhuman.

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ Since it's a built in I think you don't need the lambda. System.IO.Path.GetExtension. \$\endgroup\$
    – milk
    May 18 '17 at 0:57
  • \$\begingroup\$ @milk Of course I do, that's just a method, it won't return .ext from file.ext because with just a method there's no way of passing the file.ext in. \$\endgroup\$ May 18 '17 at 7:51
  • 1
    \$\begingroup\$ This is all you have to do. \$\endgroup\$ Mar 11 '18 at 14:50
  • \$\begingroup\$ @totallyhuman Thanks, saved another byte by dropping the trailing semi colon to. \$\endgroup\$ Mar 12 '18 at 9:03
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\$
1
  • \$\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
  • 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\$
    – Grain Ghost
    Oct 29 '18 at 20:54
1
\$\begingroup\$

Pepe, 48 bytes

rEeeEeEEEeREEeREEEerEEREEEEEEEreererEEEEeEeereee

Try it online!

\$\endgroup\$
1
\$\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\$
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\$
2
1
\$\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\$
1
\$\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\$
1
\$\begingroup\$

Pip, 10 bytes

@RV(q^".")

Splits on dots, gets first element of reverse. I wish I could make this work without the parentheses.

Try it online!

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

Keg, -hd, 4 bytes

\./÷

Try it online!

Splits on dots, and prints the last element.

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

Vyxal, 4 bytes

\.€t

Try it Online! (Test cases!)

Splits on spaces and outputs the tail.

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

sed, 14 bytes

:l
s/.\././
tl

Try it online!

Just repeatedly removes the character before a . until there's none left.

\$\endgroup\$

Your Answer

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