Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageangie 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.
****************************************
***************
Post Follow-up to this messageAngie 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
Post Follow-up to this messageOn 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
Post Follow-up to this messageThanks 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";} ? > > 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> > >
Post Follow-up to this messageangie 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 t he 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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.