CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
text does not pattern-match its own content!

 



xinelo
Novice

Mar 27, 2007, 4:34 AM

Post #1 of 5 (3424 views)
text does not pattern-match its own content! Can't Post

Hello,

I'm working on a script and I'm having difficulties with the pattern matching, surprisingly because it's the part of perl that I command better. I paste the relevant part of the script at the end of this post.

You'll see that I capture a part of the text in a variable and then, when I try to see if the text contains that variable, it doesn't!


Code
#!/usr/bin/perl  
use strict;

my $text;
while(<>) { $text=$text.$_ }

my $role;
$role = $1 if $text=~/<t:role[^>]*>([^<]+)<\/t:role>/s;
# <= there's a match here

my $role_cntxt;
if ($text=~/<t:role[^>]*>$role<\/t:role>.+?<t:role[^>]*>[^<]+<\/t:role>/) {
$role_cntxt = $&;
print "I got the context: $role_cntxt <= That was the context.\n"; # <= there was a match here too
} else { print "Didn't get the context\n"; }

if ($text=~/$role_cntxt/) { print "Match!\n" }
else { print "The context is not in the text.\n" }


Sorry, I don't think I need to send the text. For you to have an idea, it's something like:

"bla bla <t:role>a role</t:role> bla bla <t:role> another role </t:role>

Any explanations or ideas about how this can happen?

Thanks a lot, xinelo


KevinR
Veteran


Mar 27, 2007, 11:02 PM

Post #2 of 5 (3421 views)
Re: [xinelo] text does not pattern-match its own content! [In reply to] Can't Post

seems to work OK:


Code
my $text; 
while(<DATA>) { $text=$text.$_ }

my $role;
$role = $1 if $text=~/<t:role[^>]*>([^<]+)<\/t:role>/s;

my $role_cntxt;
if ($text=~/<t:role[^>]*>$role<\/t:role>.+?<t:role[^>]*>[^<]+<\/t:role>/) {
$role_cntxt = $&;
print "I got the context: $role_cntxt <= That was the context.\n"; # <= there was a match here too
} else { print "Didn't get the context\n"; }

if ($text=~/$role_cntxt/) { print "Match!\n" }
else { print "The context is not in the text.\n" }

__DATA__
bla bla <t:role>a role</t:role> bla bla <t:role> another role </t:role>
bla bla <t:role>foo bar</t:role> bla bla <t:role> blah blah</t:role>


output:

I got the context: <t:role>a role</t:role> bla bla <t:role> another role </t:role> <= That was the context.
Match!
-------------------------------------------------


xinelo
Novice

Mar 29, 2007, 1:48 AM

Post #3 of 5 (3411 views)
Re: [KevinR] text does not pattern-match its own content! [In reply to] Can't Post

Thanks KevinR,

The data example that I sent was a simplified version of my text. That's why it works. I thought there was problem with my code but in fact the problem seems to be in some characters that there were in the text, namely $, ( and ).

I replaced them in the text and now it seems to work fine. Next, I'll try whether using the option "use utf8;" in my script allows me to keep those characters in my text.

Thank you for your help.
xinelo


KevinR
Veteran


Mar 29, 2007, 12:06 PM

Post #4 of 5 (3408 views)
Re: [xinelo] text does not pattern-match its own content! [In reply to] Can't Post

You probably want to use the quotemeta() functon on the scalar before running it through the regexp:


Code
$role_cntxt= quotemeta($role_cntxt): 
if ($text=~/$role_cntxt/)



http://perldoc.perl.org/functions/quotemeta.html
-------------------------------------------------


xinelo
Novice

Mar 29, 2007, 4:23 PM

Post #5 of 5 (3404 views)
Re: [KevinR] text does not pattern-match its own content! [In reply to] Can't Post

Thank you so much Kevin.

This function seems like the thing I need. Unfortunately I won't be able to try it until mid April, when I get back from a trip. I'll send my feedback by then.

Thansk a lot and happy Easter.

Manuel

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives