Verifying a horizontal ASCII pet snake Recently there have been a couple of ASCII pet snake challenges (e.g. here)
            0 0               
  0        0 0 000            
00 0     00       000 0      0
    000 0            0 0   00 
       0                000   

This challenge is to take a randomly generated horizontal pet snake (height of five lines, length of 30), and verify that :

Each column has only one 0
Each 0 is &quot;connected&quot; to the 0 before and after it (vertically spaced by 0 or 1 line only)

Final output can be true or 1 if the snake is valid, or false or 0 if the snake is invalid
Edit—Clarification
Assume the input:

Is a string
Contains only ' ', '0', and '\n'
Has exactly 30 characters each line
Has exactly 5 lines

I.e. verify whether the snake is connected, and that there are no stray chars. No need to validate the &quot;canvas&quot; that the snake is printed on.
JavaScript (ES2018), 62 54 bytes

s=&gt;!/0(.{30}|.{60,62}(.{31})*)0|( .{30}){4} /s.test(s)


Input is a single string:


without trailing new line
contain only space, '0', and '\n'
30 characters each line, 5 lines, 154 character in total


Flag s means a dot match anything (including '\n'). This feature currently supported by Chrome 63+, Opera 50+, Safari 11.1+, based on compat-table. You may test this function with these supported browser. You will get an exception when loading the page if your browser do not support this feature.

How it works:


No column with no 0:


do not match /( .{30}){4} /

No two 0s in one column:


do not match /0.{30}(.{31})*0/

No 0 not connect to its neighbors:


do not match /0.{60}(.{31})*0/, /0.{62}(.{31})*0/



Merge all these regex, and you will finally got this one.



f =

s=&gt;!/0(.{30}|.{60,62}(.{31})*)0|( .{30}){4} /s.test(s)
&lt;textarea id=i style="width:100%;height:100px"&gt;            0 0               
  0        0 0 000            
00 0     00       000 0      0
    000 0            0 0   00 
       0                000   &lt;/textarea&gt;
&lt;button onclick="o.value=f(i.value.slice(0,154))"&gt;Validate&lt;/button&gt;
Result: &lt;output id=o&gt;&lt;/output&gt;




Thanks to Martin Ender point out that do a single ! operator may save 8 bytes.