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

This passes all your test cases:
use strict; use warnings; use Test::Simple qw(no_plan); my %test_cases = ( ' 999' => 'valid', ' 9999' => 'valid', '999999' => 'valid', ' 9' => 'valid', ' 99 9' => 'invalid', ); foreach my $case (keys %test_cases) { my $does_match = $case =~ / ^\s* # any number of leading spaces \d+ # followed by a number of digits (:? [^\s]* # but cant have any spaces after a digit has b +een found \d+ # It must contain at least one digit at the en +d )?$ /x ; ok( !($does_match xor $test_cases{$case} eq 'valid'), "'$case': $test_cases{$case}" ); }
OUTPUT:
ok 1 - '999999': valid ok 2 - ' 999': valid ok 3 - ' 9': valid ok 4 - ' 9999': valid ok 5 - ' 99 9': invalid 1..5
Bill

Replies are listed 'Best First'.
Re^2: Should be a simple spaces/digits regex....but I'm turning grey!
by Anonymous Monk on Aug 05, 2014 at 14:25 UTC

    But that matches " 999foo9", which isn't valid. Also viffer already gave the solution.

      Yes, viffer's solution and mine disagree on the validity of your string. I believe that mine is the one that meets the original specification. In hindsight, it seems that his solution probably does implement what he intendeds.
      Bill
        I believe that mine is the one that meets the original specification.

        That might be true if you believe the "original specification" was only the five example strings, and you ignore the regexes in the OP, this clarifying post, and the solution, posted six hours before your node.

        Interpreting "can't have any spaces after a digit has been found" to mean "a digit may be followed by any number of non-whitespace characters" ([^\s]*) is highly questionable on its own anyway.

        Yes, viffer's solution and mine disagree on the validity of your string.

        Since viffer is the OP, that's just a strange way of saying that your code is not actually a solution.

        In hindsight, it seems that his solution probably does implement what he intendeds.

        Another hindsight one might have is "I should have read more than 7 lines of the thread before posting", or "I could have apologized for or at least corrected my mistake instead of defending it".