in reply to Pattern matching fields with variable or NULL data

Reading all your comments, it still seems unclear exactly what you want. One thing in particular that stands out is that I can't tell if you're trying to validate data, or extract it. That is, if you're passed four digits, should it just extract three of them, or should it detect the problem? What if the string is only 2 characters long? If you can clarify exactly what you want, there are certainly ways to encode it.

It sounds like you might want /^[\d\s]{0,3}$/, which works if the length is exactly 3 characters, each a space or a digit. Another approach might be to use more than just a pattern, if (length == 3 && /^\d*\s*$/), for instance, if you want the blanks to follow all the digits. Or if you want to extract the digits part and validate, you could do something like

if (length != 3 || !/^(\d*)\s*/){ die "invalid data"; } else { print "your number without spaces is '$1'\n"; }
But again, the first step is figuring out exactly what should and shouldn't match, and what you want to do when it doesn't. After that it's easy.