Match Whole word only

P

perl Newbie

Hello ,

I am having trouble how to "match whole word only", the script I am
using here is

use warnings;
use strict;

my @lines=(
"l qa1;c=c(a0)'1'.or.c(a10)'2';&sort;&nz",
"n01 blah ... ;c=c(a9)'1'.and.c(a07)'1'",
"n01 blah ... ;c=c(a08)'1'.and.c(a11)'1'",
"*include q1.qin;kod=a05;axm=q4a4",
"*include q2.qin;pod=a06"
) ;


my @aval;
foreach my $l (@lines){
print "$l\n";
}

print "\n\n";

foreach my $l (@lines){
if ($l =~/a[0-9]+/) {
##print "$l\n";
my (@push_txt) = $l=~/(a[0-9]+)/g;
push @aval,@push_txt;
}
}

print "@aval\n";

With this I am getting all the values that qualifies condition a[0-9]+
including a1 from qa1 & a4 from q4a4.

How can I get match whole word values, in this case it should outputs
values a0 a10 a9 a07 a08 a11 a05 a06.
 
J

J. Gleixner

perl said:
Hello ,

I am having trouble how to "match whole word only", the script I am
using here is

use warnings;
use strict;

my @lines=(
"l qa1;c=c(a0)'1'.or.c(a10)'2';&sort;&nz",
"n01 blah ... ;c=c(a9)'1'.and.c(a07)'1'",
"n01 blah ... ;c=c(a08)'1'.and.c(a11)'1'",
"*include q1.qin;kod=a05;axm=q4a4",
"*include q2.qin;pod=a06"
) ;


my @aval;
foreach my $l (@lines){
print "$l\n";
}

print "\n\n";

foreach my $l (@lines){
if ($l =~/a[0-9]+/) {
##print "$l\n";
my (@push_txt) = $l=~/(a[0-9]+)/g;
push @aval,@push_txt;
}
}

print "@aval\n";

With this I am getting all the values that qualifies condition a[0-9]+
including a1 from qa1 & a4 from q4a4.

How can I get match whole word values, in this case it should outputs
values a0 a10 a9 a07 a08 a11 a05 a06.
if ($l =~/\ba[0-9]+/) {
##print "$l\n";
my (@push_txt) = $l=~/\b(a[0-9]+)/g;
push @aval,@push_txt;
}

To find out more, see: perldoc perlre
 
K

Klaus

Hello ,

I am having trouble how to "match whole word only", the script I am
using here is

use warnings;
use strict;

my @lines=(
"l qa1;c=c(a0)'1'.or.c(a10)'2';&sort;&nz",
"n01 blah ... ;c=c(a9)'1'.and.c(a07)'1'",
"n01 blah ... ;c=c(a08)'1'.and.c(a11)'1'",
"*include q1.qin;kod=a05;axm=q4a4",
"*include q2.qin;pod=a06"
) ;

my @aval;
foreach my $l (@lines){
        print "$l\n";

}

print "\n\n";

foreach my $l (@lines){
        if ($l =~/a[0-9]+/) {
                ##print "$l\n";
                my (@push_txt) = $l=~/(a[0-9]+)/g;
                push @aval,@push_txt;
        }

}

print "@aval\n";

With this I am getting all the values that qualifies condition a[0-9]+
including a1 from qa1 & a4 from q4a4.

How can I get match whole word values, in this case it should outputs
values a0 a10 a9 a07 a08 a11 a05 a06.

try \b in your regular expression:

my (@push_txt) = $l=~/\b(a[0-9]+)\b/g;
 
T

Tad J McClellan

perl Newbie said:
I am having trouble how to "match whole word only",
foreach my $l (@lines){
if ($l =~/a[0-9]+/) {
##print "$l\n";
my (@push_txt) = $l=~/(a[0-9]+)/g;
push @aval,@push_txt;
}
}


foreach my $l (@lines){
push @aval, $l =~ /\b(a[0-9]+)\b/g;
}
 
J

John W. Krahn

perl said:
I am having trouble how to "match whole word only", the script I am
using here is

use warnings;
use strict;

my @lines=(
"l qa1;c=c(a0)'1'.or.c(a10)'2';&sort;&nz",
"n01 blah ... ;c=c(a9)'1'.and.c(a07)'1'",
"n01 blah ... ;c=c(a08)'1'.and.c(a11)'1'",
"*include q1.qin;kod=a05;axm=q4a4",
"*include q2.qin;pod=a06"
) ;


my @aval;
foreach my $l (@lines){
print "$l\n";
}

print "\n\n";

foreach my $l (@lines){
if ($l =~/a[0-9]+/) {
##print "$l\n";
my (@push_txt) = $l=~/(a[0-9]+)/g;
push @aval,@push_txt;
}
}

print "@aval\n";

With this I am getting all the values that qualifies condition a[0-9]+
including a1 from qa1 & a4 from q4a4.

How can I get match whole word values, in this case it should outputs
values a0 a10 a9 a07 a08 a11 a05 a06.

$ perl -le'
my @lines = (
"l qa1;c=c(a0)\0471\047.or.c(a10)\0472\047;&sort;&nz",
"n01 blah ... ;c=c(a9)\0471\047.and.c(a07)\0471\047",
"n01 blah ... ;c=c(a08)\0471\047.and.c(a11)\0471\047",
"*include q1.qin;kod=a05;axm=q4a4",
"*include q2.qin;pod=a06",
);

foreach my $l ( @lines ) {
print $l;
}

my @aval = "@lines" =~ /\ba[0-9]+\b/g;

print "\n@aval\n";
'
l qa1;c=c(a0)'1'.or.c(a10)'2';&sort;&nz
n01 blah ... ;c=c(a9)'1'.and.c(a07)'1'
n01 blah ... ;c=c(a08)'1'.and.c(a11)'1'
*include q1.qin;kod=a05;axm=q4a4
*include q2.qin;pod=a06

a0 a10 a9 a07 a08 a11 a05 a06




John
 
S

sln

perl said:
I am having trouble how to "match whole word only", the script I am
using here is

use warnings;
use strict;

my @lines=(
"l qa1;c=c(a0)'1'.or.c(a10)'2';&sort;&nz",
"n01 blah ... ;c=c(a9)'1'.and.c(a07)'1'",
"n01 blah ... ;c=c(a08)'1'.and.c(a11)'1'",
"*include q1.qin;kod=a05;axm=q4a4",
"*include q2.qin;pod=a06"
) ;


my @aval;
foreach my $l (@lines){
print "$l\n";
}

print "\n\n";

foreach my $l (@lines){
if ($l =~/a[0-9]+/) {
##print "$l\n";
my (@push_txt) = $l=~/(a[0-9]+)/g;
push @aval,@push_txt;
}
}

print "@aval\n";

With this I am getting all the values that qualifies condition a[0-9]+
including a1 from qa1 & a4 from q4a4.

How can I get match whole word values, in this case it should outputs
values a0 a10 a9 a07 a08 a11 a05 a06.

$ perl -le'
my @lines = (
"l qa1;c=c(a0)\0471\047.or.c(a10)\0472\047;&sort;&nz",
"n01 blah ... ;c=c(a9)\0471\047.and.c(a07)\0471\047",
"n01 blah ... ;c=c(a08)\0471\047.and.c(a11)\0471\047",
"*include q1.qin;kod=a05;axm=q4a4",
"*include q2.qin;pod=a06",
);

foreach my $l ( @lines ) {
print $l;
}

my @aval = "@lines" =~ /\ba[0-9]+\b/g;

print "\n@aval\n";
'
l qa1;c=c(a0)'1'.or.c(a10)'2';&sort;&nz
n01 blah ... ;c=c(a9)'1'.and.c(a07)'1'
n01 blah ... ;c=c(a08)'1'.and.c(a11)'1'
*include q1.qin;kod=a05;axm=q4a4
*include q2.qin;pod=a06

a0 a10 a9 a07 a08 a11 a05 a06




John

print
"l qa1;c=c(a0)\0471\047.or.c(a10)\0472\047;&sort;&nz
n01 blah ... ;c=c(a9)\0471\047.and.c(a07)\0471\047
n01 blah ... ;c=c(a08)\0471\047.and.c(a11)\0471\047
*include q1.qin;kod=a05;axm=q4a4
*include q2.qin;pod=a06
" =~ /\ba[0-9]+\b/g;


c:\temp>perl ee.pl
a0a10a9a07a08a11a05a06
c:\temp>
 
S

sln

map /\ba[0-9]+\b/g, @lines

print
map /\ba[0-9]+\b/g, "l qa1;c=c(a0)\0471\047.or.c(a10)\0472\047;&sort;&nz
n01 blah ... ;c=c(a9)\0471\047.and.c(a07)\0471\047
n01 blah ... ;c=c(a08)\0471\047.and.c(a11)\0471\047
*include q1.qin;kod=a05;axm=q4a4
*include q2.qin;pod=a06
";

-sln
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top