Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > basic regex back reference

Reply
Thread Tools

basic regex back reference

 
 
gcr
Guest
Posts: n/a
 
      05-31-2006
Newbie alert. Thanks for any help.

Can't get back references - I want to pull the bracketed numbers (and
put them in an array)

#!/usr/bin/perl

use warnings;
use strict;

my $output = "5[64]790[908]90567[5678]101";
$output =~ m/(\[+[*\d]+\])/g;
print "$1\n";

prints [64]

was hoping to find [908] in $2 and [5678] in $3 ...
 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      05-31-2006
gcr wrote:
> Newbie alert. Thanks for any help.
>
> Can't get back references - I want to pull the bracketed numbers (and
> put them in an array)
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> my $output = "5[64]790[908]90567[5678]101";
> $output =~ m/(\[+[*\d]+\])/g;
> print "$1\n";
>
> prints [64]
>
> was hoping to find [908] in $2 and [5678] in $3 ...


You can use a while loop to pull out subsequent matches (dunno if
there's a proper word for that):

use warnings;
use strict;
my @array;
my $output = "5[64]790[908]90567[5678]101";
while ($output =~ m/(\[+[*\d]+\])/g) {
push @array, $1;
}
print join(",", @array), "\n";

This will print
[64],[908],[5678]

--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
 
 
 
gcr
Guest
Posts: n/a
 
      05-31-2006
In article <252e$447d55a6$c2abfc64$(E-Mail Removed)>,
Aukjan van Belkum <(E-Mail Removed)> wrote:

> my @array = ($output =~ m/(\[+[*\d]+\])/g);


works great! thanks!
(I still don't understand but the pain is gone)

The original only matches once(?)
and/or I'm not understanding the $1 $2 functionality(?)

Thanks again!
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      05-31-2006
gcr schreef:

> /(\[+[*\d]+\])/


The "\[+" means: one or more literal "["s.

The "[*\d]" is a character class off a literal asterisk and a digit, so
it tries to match either an asterisk or a digit. The "+" behoind it
means "one or more of the previous" again.

ITYM: /(\[\d+\])/
which means: a literal "[", followed by one or more digits, followed by
a literal "]".

--
Affijn, Ruud

"Gewoon is een tijger."


 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      05-31-2006
gcr wrote:
> In article <252e$447d55a6$c2abfc64$(E-Mail Removed)>,
> Aukjan van Belkum <(E-Mail Removed)> wrote:
>
>>my @array = ($output =~ m/(\[+[*\d]+\])/g);

>
> works great! thanks!
> (I still don't understand but the pain is gone)
>
> The original only matches once(?)
> and/or I'm not understanding the $1 $2 functionality(?)


$1 contains the match in the first set of parentheses and $2 contains the
match in the second set of parentheses. You only have one set of parentheses
in that pattern.


John
--
use Perl;
program
fulfillment
 
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 make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Regex back reference in gsub Dan Fitzpatrick Ruby 4 07-13-2005 10:31 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments