Matching many when valid line exists

L

Leon Williams

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]{10}[ ])*?([\d]{10})[ ]?$/

This expression seems to validate well enough but, it only matches the
last two occurrences.

Any suggestions?
 
B

Ben Morrow

Quoth Leon Williams said:
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]{10}[ ])*?([\d]{10})[ ]?$/

This expression seems to validate well enough but, it only matches the
last two occurrences.

Capture buffers with a quantifier (/(...)*/) only capture the last
occurrence. To get all of them you have to use the /g flags and match in
list context, but in this case it would be easier to use something like

my $input = '1234567890 0987654321 5678901234';
my @codes = split ' ', $input;
for (@codes) {
/\D/ and die "non-numeric code: '$_'";
length == 10 or die "bad code length: '$_'";
}

Ben
 
L

Leon Williams

Quoth Leon Williams <[email protected]>:


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]{10}[ ])*?([\d]{10})[ ]?$/
This expression seems to validate well enough but, it only matches the
last two occurrences.

Capture buffers with a quantifier (/(...)*/) only capture the last
occurrence. To get all of them you have to use the /g flags and match in
list context, but in this case it would be easier to use something like

my $input = '1234567890 0987654321 5678901234';
my @codes = split ' ', $input;
for (@codes) {
/\D/ and die "non-numeric code: '$_'";
length == 10 or die "bad code length: '$_'";
}

Ben


Your right,
I got sucked into the vortex of making something more complex (and
more interesting) then it needed to be.
Thanks the reality check.
 
J

John W. Krahn

Leon said:
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]{10}[ ])*?([\d]{10})[ ]?$/

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{10} (?= |\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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top