use ^
and $
to match the start and end of your string
^[0-9]{6}$
^[0-9]{6}\.[0-9]{3}$
Reference: http://www.regular-expressions.info/anchors.html
Also, as noted by Mikael Svenson, you can use the word boundary \b
if you are searching for this pattern in a larger chunk of text.
Reference: http://www.regular-expressions.info/wordboundaries.html
You could also write both those regexes in one shot
^\d{6}(\.\d{3})?$