g | x | w | all
Bytes Lang Time Link
054AWK250825T131012Zxrs
044GolfScript231014T000123Znoodle p
nanPerl 5 p231113T182959ZXcali
088Lua231111T215514ZKamila S
081PowerShell231113T113818ZMark Har
134Go230201T184755Zbigyihsu
066Bracmat191102T091133ZBart Jon
097Rust170717T103702Zf4bi4n
131C only calling puts170713T210214Zmichi7x7
125Common Lisp170713T180428ZRenzo
136C tcc170713T182017ZSteadybo
102Dart VM170716T202549ZIce Flak
057Java OpenJDK 8170713T160236ZOlivier
106Java/JavaScript Polyglot170713T231720ZOlivier
062Perl170713T175637ZBurgan
042Perl170716T120747Zbytepush
096Lua170716T124021ZIS4
091Batch170715T235724ZNeil
036Perl170715T210907Zstevieb
069Python 2170714T190548ZDennis
095Python 3170714T174845ZKavi
068Python 2170713T155303Zhyperneu
076Excel VBA170713T210017ZTaylor R
084Excel170714T105553ZWernisch
049JavaScript170713T155830Zuser4180
037Retina170713T211234ZDennis
nanRuby170713T233115ZValue In
036JAISBaL170713T231558ZSocratic
027Jelly170713T230707ZDennis
086 86 Bytes170713T163836ZMark
058vim170713T212317ZRay
082Mathematica170713T165250ZZaMoC
078C#170713T160311ZTheLetha
02105AB1E170713T155342ZErik the

AWK, 54 bytes

IGNORECASE=1&&$0=/^java/&&!/javascript/?"car":"carpet"

Attempt This Online!

Tried a few ways and kept getting 54:

IGNORECASE=1&&$0="car"(/^java/&&!/javascript/?X:"pet")

GolfScript, 53 49 44 bytes

{32|}%"java"/(,\{6<"script"=+}/"carpet".3<if

Try it online!

This was my second GolfScript submission so after writing a couple more I was able to revisit this and shave a couple bytes.

Previously, this mapped over a list, then summed it, then added it to a base value. Now, it just loops over the list, adding to the base value on each iteration.

Here's an explanation of how the code works:

{32|}%           # Convert the string to lowercase

"java"/          # Split on "java".
(,               # Extract the first item, and push its length
                 # (0 iff string starts with java).
\                # Swap so the rest of the list is on top of the stack.

{6<"script"=+}/  # Check if any start with "script":
{            }/  #   Loop over each item:
 6<              #     Take the first 6 characters.
   "script"=     #     Are they equal to "script"? (0 or 1)
            +    #     Add this to the top of the stack.

# After this loop, the stack will be 0 iff the string started
# with "java" and does not contain "javascript".

"carpet".3<     # Push "carpet" and its first 3 characters.
if              # If the value is 0, "car", otherwise "carpet".

Perl 5 -p, 38 + 1 = 39 bytes

$_=car.pet x!(/^java/i*!/javascript/i)

Try it online!

Lua, 88 bytes

s=io.read():lower()print(s:sub(1,4)=='java'and not s:find'javascript'and'car'or'carpet')

Attempt This Online!

PowerShell, 81 bytes

$a={param($s)$s.StartsWith('java')-and(!s.contains('javascript'))?'car':'carpet'}

Try it online!

Go, 134 bytes

import."strings"
func f(s string)string{T,o:=ToLower,"car"
if Contains(T(s),"javascript")||!HasPrefix(T(s),"java"){o+="pet"}
return o}

Attempt This Online!

Bracmat, 66 bytes

(f=.pet:?b&@(low$!arg:(? javascript ?|java ?&:?b|?))&str$(car !b))

Try it online!

Rust, 97 bytes

let s=if Regex::new(r"^javascript|^!java$").unwrap().is_match("javascript"){"carpet"}else{"car"};

I'm pretty sure that there is a shorter solution but it's my first try :)

C (only calling puts), 131 bytes

f(int*s){char r[]="carpet";~*s&'AVAJ'||(r[3]=0);for(;*s&255;*(int*)&s+=1)~*s&'AVAJ'||~s[1]&'IRCS'||~s[2]&'TP'||(r[3]='p');puts(r);}

It does have its problems, but it passes all of the testcases provided :)

g(int* s)
{
  char r[] = "carpet";
  ~*s&'AVAJ' || (r[3]=0);
  for(;*s & 255; *(int*)&s +=1)
    ~*s&'AVAJ' || ~s[1]&'IRCS' || ~s[2]&'TP' || (r[3]='p');
  puts(r);
}

Imaginary bonus points if your answer uses Java, Javascript, or Regex

well... no thanks

Common Lisp, 131 125 bytes

(lambda(s)(format()"car~@[pet~]"(or(<(length s)4)(not(#1=string-equal"java"(subseq s 0 4)))(search"javascript"s :test'#1#))))

Try it online!

Size reduced thanks to the #n= “trick” of Common Lisp.

Explanation

(lambda (s)                 ; anonymous function
  (format                   ; use of format string to produce the result
    ()                      ; the result is a string
    "car~@[pet~]"           ; print "car", then print "pet" when:
    (or (< (length s) 4)    ; the string is less then 4 characters or
        (not (string-equal "java" (subseq s 0 4)))     ; does not start with java or
        (search "javascript" s :test 'string-equal)))) ; contains javascript

C (tcc), 144 136 bytes

a;f(s){char*t=s;for(;*t;a=!strncmp(s,"java",4))*t=tolower(*t++);for(t=s;*t;)s=strncmp(t++,"javascript",10)&&s;puts(a*s?"car":"carpet");}

Try it online!

Unrolled:

a;
f(s)
{
    char *t = s;
    for (; *t; a = !strncmp(s, "java", 4))
        *t = tolower(*t++);
    for (t=s; *t;)
        s = strncmp(t++, "javascript", 10) && s;
    puts(a*s ? "car"  :"carpet");
}

Dart VM, 104 bytes 102 bytes

main(p){p=p[0].toLowerCase();print("car${p.indexOf('java')==0&&p.indexOf('javascript')<0?'':'pet'}");}

Explanation:

Degolfed:

main(p)
{
    p = p[0].toLowerCase();
    print("car${p.indexOf('java') == 0 && p.indexOf('javascript') < 0 ? '' : 'pet'}");
}

We have our usual main function

We replace p with p[0].toLowerCase(); so that we don't have to declare a new variable (var plus a space would be 4 extra bytes)

We then proceed to do the actual printing

We print car unconditionally and we use inline statements for checking whether to print pet after it or not. If it has the string 'java' at index 0 and does not have 'javascript' in it, we do nothing (we actually append an empty string but it does not have any effect) and otherwise we append pet.

Java (OpenJDK 8), 92 82 72 58 57 bytes

s->s.matches("(?i)(?!.*javascript)java.*")?"car":"carpet"

Try it online!

1 byte saved thanks to @Nevay!

Java/JavaScript Polyglot, 108 107 106 bytes

//\u000As->s.matches("(?i)(?!.*javascript)java.*"/*
a=>/(?!.*javascript)^java/i.test(a/**/)?"car":"carpet"

Run as Java

//\u000As->s.matches("(?i)(?!.*javascript)java.*"/*
a=>/(?!.*javascript)^java/i.test(a/**/)?"car":"carpet"

Try it online!

Note: don't trust the highlight as it's incorrect. The real Java, properly interpreted looks like below because \u000A is interpreted in the very first step of the compilation as \n, de facto ending the comment that started with the line comment (//).

//
s->s.matches("(?i)(?!.*javascript)java.*"/*
a=>/(?!.*javascript)^java/i.test(a/**/)?"car":"carpet"

Run as JavaScript

//\u000As->s.matches("(?i)(?!.*javascript)java.*"/*
a=>/(?!.*javascript)^java/i.test(a/**/)?"car":"carpet"

Credits to @CowsQuak for the JS version.

let f=

//\u000As->s.matches("(?i)(?!.*javascript)java.*"/*
a=>/(?!.*javascript)^java/i.test(a/**/)?"car":"carpet"

var a=["java","javafx","javabeans","java-stream","java-script","java-8","java.util.scanner","javascript","java-avascript","javascript-events","facebook-javascript-sdk","javajavascript","jquery","python","rx-java","java-api-for-javascript","not-java"];

for(var s of a) console.log(s.padStart(a.reduce((x,y)=>x.length>y.length?x:y).length) + "=>" + f(s));

How many imaginary bonus points for this answer?

-1 byte thanks to @Nevay in the Java answer.

Perl, 98 84 62 Bytes

sub a{"car".($_[0]=~/javascript/i||$_[0]!~/^java/i?'pet':'');}

Try it online!

Thanks to bytepusher

Perl, 42 bytes

I believe the answer by stevieb has an incorrect output (tried that one myself - it returns car for 'javajavascript'). This should work:

say/^java/i&&!/javascript/i?'car':'carpet'

Lua, 96 bytes

function(x)return x:lower():match"^java"and not x:lower():match"javascript"and"car"or"carpet"end

Batch, 91 bytes

@set/ps=
@set t=%s:~0,4%
@if "%t:java=%%s:javascript=%"=="%s%" (echo car)else echo carpet

Takes input on STDIN. Batch doesn't have a case insensitive comparison operator but it does have case insensitive string replacement so I can assign a temporary to the first four characters and then case insensitively replace java, which should then result in the empty string. Meanwhile I case insensitively replace javascript in the original string, which should leave it unchanged.

Perl, 36 bytes

say/^java(?!script)/i?"car":"carpet"

Run it as such:

perl -nE 'say/^java(?!script)/i?"car":"carpet"' java.txt

Python 2, 69 bytes

f=input().lower().find
print'car'+'pet'*(f('java')!=~f('javascript'))

Currently 1 byte longer than the shortest Python 2 solution.

Try it online!

Python 3, 95 bytes

g=lambda s:(lambda r:'car' if r[:4]=='java' and 'javascript' not in r else 'carpet')(s.lower())

Try it online!

Yeah, it could be shorter but I wanted to try using a nested lambda!

Python 2, 68 bytes

k=input().lower();print'car'+'pet'*(k[:4]!='java'or'javascript'in k)

Try it online!

-11 bytes thanks to notjagan
-2 bytes thanks to Dennis

Excel VBA, 76 Bytes

Anonymous VBE immediate window function that takes input from range [A1] and outputs is car/carpet status to the VBE immediate window

Does not use RegExp

?"car"IIf(InStr(1,[A1],"Java",1)*(InStr(1,[A1],"JavaScript",1)=0),"","pet")

Excel, 84 bytes

="car"&IF(AND(ISERR(SEARCH("javascript",A1)),IFERROR(SEARCH("java",A1),2)=1),,"pet")

JavaScript, 50 49 bytes

Saved 1 byte thanks to @ValueInk by rearranging the regex

a=>/javascript|^(?!java)/i.test(a)?"car":"carpet"

Test snippet

let f=

a=>/javascript|^(?!java)/i.test(a)?"carpet":"car"

var a=["java","javafx","javabeans","java-stream","java-script","java-8","java.util.scanner","java-avascript","javascript","javascript-events","facebook-javascript-sdk","javajavascript","jquery","python","rx-java","java-api-for-javascript","not-java"];

for(var s of a) console.log(s.padStart(a.reduce((x,y)=>x.length>y.length?x:y).length) + "=>" + f(s));

Retina,  44  37 bytes

Ai`^(?!.*javascript)java
.+
pet
^
car

Thanks to @MartinEnder for golfing off 7 bytes!

Try it online!

Ruby, 42+1 = 43 bytes

Uses the -p flag.

$_="car#{"pet"if~/javascript|^(?!java)/i}"

Try it online!

JAISBaL, 36 bytes

℠℘(?!.*javascript)^java.*}͵Ucar½Upet

Verbose/explanation:

# \# enable verbose parsing #\
tolower                           \# [0] convert the top value of the stack to lowercase #\
split (?!.*javascript)^java.*}    \# [1] split the top value of the stack by (?!.*javascript)^java.*} #\
arraylength                       \# [2] push the length of the array onto the stack #\
print3 car                        \# [3] print car #\
!if                               \# [4] if the top value on the stack is falsy, skip the next statement #\
print3 pet                        \# [5] print pet #\

JAISBaL was my first attempt at designing a golfing language, so it's rather quirky... there's no matches or contains, regex or otherwise, so instead we have to split and check the resulting array length... because JAISBaL has a split-by-regex... but no other regex support.... because reasons.

Regex stolen borrowed from @Cows Quack's answer.

Jelly, 27 bytes

,“Ẋṣ“®Ẓȷ»ŒlwЀ/Ḅn2‘×3“¢Ẹị»ḣ

Try it online!

EXCEL Google Sheets, 89 86 Bytes

Saved 3 bytes thanks to Taylor Scott

=LEFT("carpet",6-3*ISERR(SEARCH("javascript",A1))+3*ISERR(IF(SEARCH("java",A1)=1,1,1/0

Takes an input on A1

Explanation

=LEFT("carpet",6-3*ISERR(SEARCH("javascript",A1))+3*ISERR(IF(SEARCH("java",A1)=1,1,1/0)))

 SEARCH("javascript",A1)        #Case-Insensitive Find, returns error if not found  
 ISERR(                         #Returns string true if error, False if not
 3*ISERR(                       #Forces TRUE/False as integer, multiplies by 3
 IF(SEARCH("java",A1)=1,1,1/0)  #If java found, returns integer. if 1, java begins string
                                #so returns 1, which will be turned into 0 by iserr.
                                #Else returns 1/0, which will be turned into 1 by iserr.
 LEFT(                          #Returns digits from the left, based upon count.

vim, 58 bytes

gUU:s/.*JAVASCRIPT.*/Q/g
:s/^JAVA.*/car
:s/[A-Z].*/carpet

Try it online!

Mathematica, 82 bytes

regex

If[#~StringMatchQ~RegularExpression@"(?i)(?!.*javascript)^java.*","Car","Carpet"]&

C#, 80 78 bytes

s=>(s=s.ToLower()).StartsWith("java")&!s.Contains("javascript")?"car":"carpet"

05AB1E, 21 bytes

lD'¦‚å≠sη'îáå*„¾„ƒ´#è

Try it online!