Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Matching many when valid line exists

Reply
Thread Tools

Matching many when valid line exists

 
 
Leon Williams
Guest
Posts: n/a
 
      03-28-2008
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?
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      03-28-2008

Quoth Leon Williams <(E-Mail Removed)>:
> 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

 
Reply With Quote
 
 
 
 
Leon Williams
Guest
Posts: n/a
 
      03-28-2008
On Mar 27, 11:15 pm, Ben Morrow <(E-Mail Removed)> wrote:
> Quoth Leon Williams <(E-Mail Removed)>:
>
>
>
> > 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.
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      03-28-2008
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]{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
--
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check if a directory exists? folder.exists() does not work! Ulf Meinhardt Java 8 08-28-2009 12:26 PM
Help with Pattern matching. Matching multiple lines from while reading from a file. Bobby Chamness Perl Misc 2 05-03-2007 06:02 PM
finding if file exists or not in apache webdav server and if exists what is the mime type of it.... Totan Java 0 04-17-2006 05:13 AM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 PM



Advertisments