cannot match pattern

  • Thread starter Domenico Discepola
  • Start date
D

Domenico Discepola

Hello. I cannot match $ex in the following example. I'm reading records
from a text file (field seperator "\n" record seperator "\x0c"). I
simulated 1 incoming record in the $a variable. Unfortunately, I wasn't
able to get any insight from perldoc... Any help would be appreciated.

#!perl
use strict;
use warnings;

my $ex = '$Conflict: ';
my $a = "field1: test1\n\$Conflict: 1\nfield 3: test3\x0c";
print "[$a]\n";

if ( $a =~ /$ex/ ) {
print "found\n";
} else {
print "not found\n";
}
 
B

Ben Morrow

Domenico Discepola said:
Hello. I cannot match $ex in the following example. I'm reading records
from a text file (field seperator "\n" record seperator "\x0c"). I
simulated 1 incoming record in the $a variable. Unfortunately, I wasn't
able to get any insight from perldoc... Any help would be appreciated.

#!perl
use strict;
use warnings;

my $ex = '$Conflict: ';
my $a = "field1: test1\n\$Conflict: 1\nfield 3: test3\x0c";
print "[$a]\n";

if ( $a =~ /$ex/ ) {
print "found\n";
} else {
print "not found\n";
}

Have you run this code? It works for me.

As a side issue, it's best not to use $a or $b: see perlvar.

Ben
 
D

Domenico Discepola

Ben Morrow said:
Domenico Discepola said:
Hello. I cannot match $ex in the following example. I'm reading records
from a text file (field seperator "\n" record seperator "\x0c"). I
simulated 1 incoming record in the $a variable. Unfortunately, I wasn't
able to get any insight from perldoc... Any help would be appreciated.

#!perl
use strict;
use warnings;

my $ex = '$Conflict: ';
my $a = "field1: test1\n\$Conflict: 1\nfield 3: test3\x0c";
print "[$a]\n";

if ( $a =~ /$ex/ ) {
print "found\n";
} else {
print "not found\n";
}

Have you run this code? It works for me.

As a side issue, it's best not to use $a or $b: see perlvar.

Ben

Yes - I ran this code on my w2k and redhat boxes and perl still does not
match the expression...
Point taken on the $a $b vars...
 
T

Tad McClellan

Domenico Discepola said:
I cannot match $ex in the following example.
my $ex = '$Conflict: ';
if ( $a =~ /$ex/ ) {
print "found\n";


Your pattern says that you want to match "Conflict: " _after_
the end of the string.

That is never going to happen.

my $ex = '\$Conflict: ';

or

if ( $a =~ /\Q$ex/ ) {
 
B

Ben Morrow

Domenico Discepola said:
Ben Morrow said:
Domenico Discepola said:
Hello. I cannot match $ex in the following example. I'm reading records
from a text file (field seperator "\n" record seperator "\x0c"). I
simulated 1 incoming record in the $a variable. Unfortunately, I wasn't
able to get any insight from perldoc... Any help would be appreciated.

#!perl
use strict;
use warnings;

my $ex = '$Conflict: ';
my $a = "field1: test1\n\$Conflict: 1\nfield 3: test3\x0c";
print "[$a]\n";

if ( $a =~ /$ex/ ) {
print "found\n";
} else {
print "not found\n";
}

Have you run this code? It works for me.

Yes - I ran this code on my w2k and redhat boxes and perl still does not
match the expression...

My apologies: I am an idiot. I made the reverse of the usual mistake
and retyped my test, with a typo... :(

The trouble is that your regex comes out as /$Conflict: /, which
attempts to match 'end-of-string, followed by Conflict: '. This
doesn't match. What you want is

if ( $a =~ /\Q$ex/ ) {

which will quote the $ for you. \Q is documented in perlop, under
"Quote and Quote-like Operators".

Ben
 
G

Glenn Jackman

Domenico Discepola said:
my $ex = '$Conflict: ';
my $a = "field1: test1\n\$Conflict: 1\nfield 3: test3\x0c";
print "[$a]\n";

if ( $a =~ /$ex/ ) {

The dollar sign in $ex is the problem. Perl's trying to match
'Conflict: ' after the end of the line.

use: $ex = '\$Conflict: ';
or: $ex = quotemeta '$Conflict: ';
or: $a =~ /\Q$ex/
 
G

gilgames

my $ex = '\$Conflict: ';
my $a = "field1: test1\n\$Conflict: 1\nfield 3: test3\x0c";
print "[$a]\n";

if ( $a =~ /$ex/ ) {
print "found\n";
} else {
print "not found\n";}

This is the correct. The change is in the first line here :

my $ex = '\$Conflict: ';

This way returns

[field1: test1
$CONFLICT: 1
field 3: test3 ]
found
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top