Click here to get back home

Matching many when valid line exists

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Matching many when valid line exists Leon Williams 03-27-2008
Posted by Leon Williams on March 27, 2008, 10:56 pm
Please log in for more thread options
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?

Posted by Ben Morrow on March 27, 2008, 11:15 pm
Please log in for more thread options

> 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.

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


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

Similar ThreadsPosted
FAQ 6.2: I'm having trouble matching over more than one line. What's wrong? December 22, 2004, 12:03 pm
FAQ 6.2 I'm having trouble matching over more than one line. What's wrong? January 20, 2005, 6:03 pm
FAQ 6.2 I'm having trouble matching over more than one line. What's wrong? March 31, 2005, 12:03 pm
matching multiple occurrences in the same line April 27, 2005, 12:20 pm
FAQ 6.2 I'm having trouble matching over more than one line. What's wrong? June 16, 2005, 5:03 pm
FAQ 6.2 I'm having trouble matching over more than one line. What's wrong? September 30, 2005, 4:03 pm
FAQ 6.2 I'm having trouble matching over more than one line. What's wrong? November 3, 2005, 11:03 am
Matching spaces at start of line November 24, 2005, 9:30 pm
FAQ 6.2 I'm having trouble matching over more than one line. What's wrong? December 1, 2005, 11:03 pm
FAQ 6.2 I'm having trouble matching over more than one line. What's wrong? August 26, 2006, 3:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap