Saravana Kumar wrote:
> Hi list,

Hello,

> I am testing a regex with email ids. I have a list of ids that i want to
> match against a one more list of ids.
> 
> I have this:
> #! /usr/bin/perl

use warnings;
use strict;


> $id="[EMAIL PROTECTED]";

Scalars and arrays are interpolated in double quoted strings so after
interpolation your string looks like 'user1.net' which is what is displayed in
your 'not found' message below.


> while(<>) {
> chomp($_);print "$_\t";
> print "$id found\n" if  /$id/;
> print "$id not found\n" if ! /$id/;
> }

If you are going to use $id in a regular expression you need to use quotemeta
to ensure that the . matches a literal period and you should use anchors so
that '[EMAIL PROTECTED]' doesn't match '[EMAIL PROTECTED]' for example.

print "$_\t$id ", /\A\Q$id\E\z/ ? '' : 'not ', "found\n";

But if you are looking for an exact match then you shouldn't be using a
regular expression at all:

print "$_\t$id ", $_ eq $id ? '' : 'not ', "found\n";


> and a file /tmp/sampleids
> [EMAIL PROTECTED]
> [EMAIL PROTECTED]
> [EMAIL PROTECTED]
> [EMAIL PROTECTED]
> 
> When i run it i get : 
> [EMAIL PROTECTED]       user1.net not found
> [EMAIL PROTECTED]       user1.net not found
> [EMAIL PROTECTED]       user1.net not found
> [EMAIL PROTECTED]     user1.net not found



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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to