Hi all.
I'm hoping someone can point out the error of my ways... I'm sure there is a simple regex for this but I'm buggered if I can figure it out.

I am trying to match a field that may have a number of leading spaces, followed by a number of digits, but can't have any spaces after a digit has been found. It must contain at least one digit at the end,
i.e. valid data may be
'     999' or '   9999' or '9999999' '      9'.

but can't be

'  99  9'.

I have a regex which works

\S{7}|\s{6}\d{1}|\s{5}\d{2}|\s{4}\d{3}|\s{3}\d{4}|\s{2}\d{5}|\s{1}\d{6 +}|\d{7}
but when you're checking a 16 byte filed

ZZZZZZZZZZZZ9V99

a regex of

(\s{12}\d{1}|\s{11}\d{2}|\s{10}\d{3}|\s{9}\d{4}|\s{8}\d{5}|\s{7}\d{6}| +\s{6}\d{7}|\s{5}\d{8}|\s{4}\d{9}|\s{3}\d {10}|\s{2}\d{11}|\s{1}\d{12}|\d{13})\.\d{2}
whilst doing the job,is getting ludicrously large and unreadable

There must be a shorter regex that covers this?

Someone at work suggested using an sprintf in it's stead within the regex, but I must be honest and say that suggestion has left me clueless on how to do it.

Thanks for your time


In reply to Should be a simple spaces/digits regex....but I'm turning grey! by viffer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.