|
Posted by John W. Krahn on March 27, 2008, 11:39 pm
Please log in for more thread options Leon Williams wrote:
> I am pulling my hair out trying to make a regex that will
> 1) Validate an entire line of input
> 2) Return any number of matches in the line
>
> The condition is that any number of product codes must exist on a line
> separated a space. It may or may not start or end with spaces. The
> product code is a 10 digit number.
>
> Example Valid Input:
> "1234567890 0987654321 5678901234"
>
> Current Expression:
> /^[ ]?([\d][ ])*?([\d])[ ]?$/
>
> This expression seems to validate well enough but, it only matches the
> last two occurrences.
>
> Any suggestions?
$ perl -le'
for ( " 1234567890 0987654321 5678901234 ", " 1234567890 ", " ", " 12345
" ) {
$count = @matches = / (?<=\A| ) \d (?= |\z) /xg;
print qq["$_" ], $count ? "matched @matches." : "did not match.";
}
'
" 1234567890 0987654321 5678901234 " matched 1234567890 0987654321
5678901234.
" 1234567890 " matched 1234567890.
" " did not match.
" 12345 " did not match.
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
|