Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

regex lying to me
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

Report this thread to moderator Post Follow-up to this message
Old Post
Angie Ahl
04-20-05 01:55 AM


RE: regex lying to me
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.
 ****************************************
***************


Report this thread to moderator Post Follow-up to this message
Old Post
Wagner, David --- Senior Programmer Analyst --- WG
04-20-05 01:55 AM


Re: regex lying to me
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

Report this thread to moderator Post Follow-up to this message
Old Post
thundergnat
04-20-05 01:55 AM


Re: regex lying to me
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

Report this thread to moderator Post Follow-up to this message
Old Post
Offer Kaye
04-20-05 01:55 AM


Re: regex lying to me
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";} 
? 
>
> 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>
>
>

Report this thread to moderator Post Follow-up to this message
Old Post
Angie Ahl
04-20-05 08:56 PM


Re: regex lying to me
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 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

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
04-20-05 08:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:19 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.