For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2005 > regex lying to me









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author regex lying to me
Angie Ahl

2005-04-19, 8:55 pm

The following regex is failing strangely:

=09my @tables =3D $content =3D~ m#\[table\](.*?)\[/table\]#g;
=09foreach (@tables) {
=09=09my $table =3D $_;
=09=09if ($content =3D~ m#$table#) {print "yes old table is there!\n";}
=09}

@tables contains 2 items (correctly) but seaching for each item in
$content does not match.

How can it find 2 matches and then claim that each of them aren't there?

Perl 5.8.6 Mac OS X 10.3.8

Thanks

bemused Angie
Wagner, David --- Senior Programmer Analyst --- WG

2005-04-19, 8:55 pm

angie ahl wrote:
> The following regex is failing strangely:
>=20
> my @tables =3D $content =3D~ m#\[table\](.*?)\[/table\]#g;
> foreach (@tables) {
> my $table =3D $_;
> if ($content =3D~ m#$table#) {print "yes old table is there!\n";}
> }
>=20
> @tables contains 2 items (correctly) but seaching for each item in
> $content does not match.
>=20
> How can it find 2 matches and then claim that each of them aren't
> there?=20
>=20
> Perl 5.8.6 Mac OS X 10.3.8

I am running AS 5.8.3 build 809.

Here is the script I ran and it worked:
#!perl

use strict;
use warnings;
use Data::Dumper;
my $content =3D <<'MYCONTENT';
[table] 1212312321 [/table]
[table] 123213312 [/table]

MYCONTENT

my @tables =3D $content =3D~ m#\[table\](.*?)\[/table\]#g;
print Dumper(\@tables);
=20
foreach (@tables) {
my $table =3D $_;
if ($content =3D~ m#$table#) {print "yes old table is there!\n";}
}

Output:
$VAR1 =3D [
' 1212312321 ',
' 123213312 '
];
yes old table is there!
yes old table is there!

Wags ;)
>=20
> Thanks
>=20
> bemused Angie




****************************************
***************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************
***************

thundergnat

2005-04-19, 8:55 pm

Angie Ahl wrote:
> The following regex is failing strangely:
>
> my @tables = $content =~ m#\[table\](.*?)\[/table\]#g;
> foreach (@tables) {
> my $table = $_;
> if ($content =~ m#$table#) {print "yes old table is there!\n";}
> }
>
> @tables contains 2 items (correctly) but seaching for each item in
> $content does not match.
>
> How can it find 2 matches and then claim that each of them aren't there?
>
> Perl 5.8.6 Mac OS X 10.3.8
>
> Thanks
>
> bemused Angie


I rather suspect that either $content or $table does not contain
what you think it does.

After substituting in some junk values, it works as expected for me.

my $content = '[table]one[/table]l;sjkdnf[table]two[/table]';
my @tables = $content =~ m#\[table\](.*?)\[/table\]#g;
foreach (@tables) {
my $table = $_;
print "$table - ";
if ($content =~ m#$table#) {print "yes old table is there!\n";}
}

Perl 5.8.6, Win2k
Offer Kaye

2005-04-19, 8:55 pm

On 4/19/05, angie ahl wrote:
> The following regex is failing strangely:
>=20
> my @tables =3D $content =3D~ m#\[table\](.*?)\[/table\]#g;
> foreach (@tables) {
> my $table =3D $_;
> if ($content =3D~ m#$table#) {print "yes old table is the=

re!\n";}
> }
>=20
> @tables contains 2 items (correctly) but seaching for each item in
> $content does not match.
>=20
> How can it find 2 matches and then claim that each of them aren't there?
>=20
> Perl 5.8.6 Mac OS X 10.3.8
>=20
> Thanks
>=20
> bemused Angie
>=20


Metachars in the results perhaps? Have you tried dumping @tables using
Data::Dumper and looking at the results?
Try using quotemeta (http://perldoc.perl.org/functions/quotemeta.html):
my $table =3D quotemeta;

HTH,
--=20
Offer Kaye
Angie Ahl

2005-04-20, 3:56 pm

Thanks

I know that there are metachars in the result. non printing chars that
I need to leave there until a later part of the processing.

I don't get why this would affect the result though.

Cheers

Angie

> On 4/19/05, angie ahl wrote:
here!\n";}[color=darkred]
?[color=darkred]
>
> Metachars in the results perhaps? Have you tried dumping @tables using
> Data::Dumper and looking at the results?
> Try using quotemeta (http://perldoc.perl.org/functions/quotemeta.html):
> my $table =3D quotemeta;
>
> HTH,
> --
> Offer Kaye
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>

John W. Krahn

2005-04-20, 3:56 pm

angie ahl wrote:
>
>
> I know that there are metachars in the result. non printing chars that


Meta-characters are not non-printing characters.


> I need to leave there until a later part of the processing.
>
> I don't get why this would affect the result though.


Say that you have the string 'ab+c', that is the character 'a' followed by the
character 'b' followed by the character '+' followed by the character 'c'. If
you use that as a regular expression it says match an 'a' followed by one or
more 'b's followed by a 'c' so the regular expression 'ab+c' will match 'abc'
or 'abbc' or 'abbbbbc' but it will never match the string 'ab+c'.


John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com