What size is the digit?
Ruby, 250
-&gt;x{d=y=0
x.size.downto(0){|n|y=n
a=[&quot;|
&quot;*2*n]
&quot;XNRDqpm@A&quot;.bytes{|z|p=[?|,' ','']
h=s=&quot;&quot;
(n*2).times{|i|
i%n&lt;1&amp;&amp;(d=z&gt;&gt;i/n*3&amp;7)&amp;&amp;h=[?_,' '][d/3%2]*n
s=p[d%3]+h+p[d/6]+&quot;
&quot;+s
h=' '*n}
z!=68&amp;&amp;s=' '*(1-d%3/2)+?_*n+&quot; 
&quot;+s
a&lt;&lt;s};puts a
a.index(x)&amp;&amp;break}
y}

Given that there are so many possible invalid inputs, the only way to do this was to generate all the correct digits and check to see if the input matches.
I build each digit up from bottom to top, in 2 halves plus the top line. Although there are 12 possibilities (considering that the left segment can be on, off, or in the case of 3and 7completely absent) only 7 are actually present and careful choice of encoding enables all info (except the top line) to be encoded into a single character.
the digit 1 doesn't really fit the pattern and is handled separately, being used to initialize the array.
Ungolfed in test program
This version uses . instead of spaces for diagnostic reasons.
#Encoding used for half-digits (radix 3,2,2 most significant digit at right)

#000    |_|  0

#100    ._|  1  . = space

#200    X_|  2  X = no space (for digits 3 and 7)  

#010    |.|  3

#110    ..|  4

#210    X.|  5

#001    |_.  6


f=-&gt;x{d=y=0                                        #d and y required to be intialized for scoping reasons
  x.size.downto(0){|n|y=n                          #Assume max possible size of character = length of input and iterate down through all possible sizes n   
    a=[&quot;|\n&quot;*2*n]                                  #Make an array containing the digit 1 (different shape to others)
    &quot;XNRDqpm@A&quot;.bytes{|z|                          #Each character encodes the pattern for a digit. Iterate through them
      p=['|','.','']                               #Possible components for left and right of digit
      h=s=&quot;&quot;                                       #h initialized for scoping reasons. s will contain the digit string 
      (n*2).times{|i|                              #For each row
        i%n&lt;1&amp;&amp;                                    #If i%n==1 we are at the bottom of a half digit
        (d=z&gt;&gt;i/n*3&amp;7)&amp;&amp;                           #so extract info from z and store in d
        h=[?_,'.'][d/3%2]*n                        #h is the horizontal part of the half digit, either _ or spaces 
        s=p[d%3]+h+p[d/6]+&quot;\n&quot;+s                   #Build one row of digit, working upwards: left,middle,right
        h='.'*n                                    #If row i%n!=0 (not bottom row of half digit)the middle section must contain spaces
      }                                            #We now have both halves of the digit, only the top segment missing 
      z!=68&amp;&amp;s='.'*(1-d%3/2)+?_*n+&quot;.\n&quot;+s          #If z!=68 (digit 4) add a top to the digit, with appropriate leading and trailing spaces        
      a&lt;&lt;s                                         #Add the completed digit of size n to a
    }
    #puts a                                        #Diagnostic: uncomment to print all the strings checked
    a.index(x)&amp;&amp;break                              #If string x is in a, break
        
  }
y                                                  #and return last value of n 
}


# digit 7, size 2. Trailing newline required. Outputs 2
puts f[
&quot;__.
..|
..|
..|
..|
&quot;]