Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > regex in perl (using variables)

Reply
Thread Tools

regex in perl (using variables)

 
 
dario
Guest
Posts: n/a
 
      08-11-2005
How do I make this work!!!

$head_ ="Subject: Get cheap v i a g r a ..... ";

#$rule is a variable which I used for reading text from a file!
open (NWRULE, "<rule.spam");
@new_rule=<NWRULE>;
close (NWRULE);

Then I did sometning like this :
foreach $rule(@new_rule)
{
if($rule =~ /(\S+) (\S+) ([^\n]+)/)
{
$new_id=$1;
$dio=$2;
$reg=$3;
if($head_ =~ m/$reg/)
{
print "something\n";
}
.....
Content of a file rule.spam is :
new_1 head Subject: .*\.\.


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-11-2005
dario wrote:
> How do I make this work!!!


<fragmentary code snipped>

Please post a _short_ but _complete_ program that illustrates the
problem you are having, just as is explained in the posting guidelines
for this group.
http://mail.augustmail.com/~tadmc/cl...uidelines.html

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
dario
Guest
Posts: n/a
 
      08-11-2005
Sorry about the previuos post, i hope this is better!
I want to match regex stored in a file to a text in a variable $head_. It
works in windows, but not on linux.
Thanks!
Dario

Content of a file rule.spam is :
new_1 head Subject: .*\.\.

Code is:

$head_ ="Subject: Get cheap v i a g r a ..... ";

open (NWRULE, "<rule.spam");
@new_rule=<NWRULE>;
close (NWRULE);

foreach $rule(@new_rule)
{
if($rule =~ /(\S+) (\S+) ([^\n]+)/)
{
$new_id=$1;
$dio=$2;
$reg=$3;
}
if($head_ =~ m/$reg/)
{
print "something\n";# it doesn't match
}
}


 
Reply With Quote
 
vsnadagouda@gmail.com
Guest
Posts: n/a
 
      08-11-2005
Hello Dario,

I tried your piece of code on Linux box ( with perl version 5.6 and
got expected results. Why do not you give a try to get the latest
verson of the perl and then test.

Cheers
-Vallabha

dario wrote:
> Sorry about the previuos post, i hope this is better!
> I want to match regex stored in a file to a text in a variable $head_. It
> works in windows, but not on linux.
> Thanks!
> Dario
>
> Content of a file rule.spam is :
> new_1 head Subject: .*\.\.
>
> Code is:
>
> $head_ ="Subject: Get cheap v i a g r a ..... ";
>
> open (NWRULE, "<rule.spam");
> @new_rule=<NWRULE>;
> close (NWRULE);
>
> foreach $rule(@new_rule)
> {
> if($rule =~ /(\S+) (\S+) ([^\n]+)/)
> {
> $new_id=$1;
> $dio=$2;
> $reg=$3;
> }
> if($head_ =~ m/$reg/)
> {
> print "something\n";# it doesn't match
> }
> }


 
Reply With Quote
 
dario
Guest
Posts: n/a
 
      08-11-2005
Thanks, I'll try.
<> wrote in message
news: oups.com...
> Hello Dario,
>
> I tried your piece of code on Linux box ( with perl version 5.6 and
> got expected results. Why do not you give a try to get the latest
> verson of the perl and then test.
>
> Cheers
> -Vallabha
>
> dario wrote:
> > Sorry about the previuos post, i hope this is better!
> > I want to match regex stored in a file to a text in a variable $head_.

It
> > works in windows, but not on linux.
> > Thanks!
> > Dario
> >
> > Content of a file rule.spam is :
> > new_1 head Subject: .*\.\.
> >
> > Code is:
> >
> > $head_ ="Subject: Get cheap v i a g r a ..... ";
> >
> > open (NWRULE, "<rule.spam");
> > @new_rule=<NWRULE>;
> > close (NWRULE);
> >
> > foreach $rule(@new_rule)
> > {
> > if($rule =~ /(\S+) (\S+) ([^\n]+)/)
> > {
> > $new_id=$1;
> > $dio=$2;
> > $reg=$3;
> > }
> > if($head_ =~ m/$reg/)
> > {
> > print "something\n";# it doesn't match
> > }
> > }

>



 
Reply With Quote
 
dario
Guest
Posts: n/a
 
      08-11-2005
I'm using 5.8.4. It's newer than yours!!!
<> wrote in message
news: oups.com...
> Hello Dario,
>
> I tried your piece of code on Linux box ( with perl version 5.6 and
> got expected results. Why do not you give a try to get the latest
> verson of the perl and then test.
>
> Cheers
> -Vallabha
>
> dario wrote:
> > Sorry about the previuos post, i hope this is better!
> > I want to match regex stored in a file to a text in a variable $head_.

It
> > works in windows, but not on linux.
> > Thanks!
> > Dario
> >
> > Content of a file rule.spam is :
> > new_1 head Subject: .*\.\.
> >
> > Code is:
> >
> > $head_ ="Subject: Get cheap v i a g r a ..... ";
> >
> > open (NWRULE, "<rule.spam");
> > @new_rule=<NWRULE>;
> > close (NWRULE);
> >
> > foreach $rule(@new_rule)
> > {
> > if($rule =~ /(\S+) (\S+) ([^\n]+)/)
> > {
> > $new_id=$1;
> > $dio=$2;
> > $reg=$3;
> > }
> > if($head_ =~ m/$reg/)
> > {
> > print "something\n";# it doesn't match
> > }
> > }

>



 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-11-2005
dario wrote:
> Sorry about the previuos post, i hope this is better!


What previous post? Please quote some context when posting a follow
up.

> I want to match regex stored in a file to a text in a variable $head_. It
> works in windows, but not on linux.


I fail to believe that.

> Thanks!
> Dario
>
> Content of a file rule.spam is :
> new_1 head Subject: .*\.\.
>
> Code is:
>
> $head_ ="Subject: Get cheap v i a g r a ..... ";


You are not using strict. I am willing to bet you are also not using
warnings. Please add these lines to your code:
use strict;
use warnings;

>
> open (NWRULE, "<rule.spam");


You are not checking to see if this open actually succeeded. For all
you know, this file never opened, and therefore the below loop was
never executed.

open my $NWRULE, '<' 'rule.spam' or die "Could not open rule.spam: $!";

> @new_rule=<NWRULE>;
> close (NWRULE);
>
> foreach $rule(@new_rule)


Please don't do this. There is no reason to store the entire file in
memory, only to loop through it moments later.

Simply process the file line by line:

while (my $rule = <$NWRULE>) {

> {
> if($rule =~ /(\S+) (\S+) ([^\n]+)/)


The . wildcard already means "anything but the newline". No reason to
create the character class:

if ($rule =~ /(\S+) (\S+) (.+)/)

> {
> $new_id=$1;
> $dio=$2;
> $reg=$3;
> }
> if($head_ =~ m/$reg/)
> {
> print "something\n";# it doesn't match


Have you bothered printing the contents of either $head or $reg to
confirm they are what you think they are?

> }
> }


Please modify your script so that it produces some debugging output, is
strict- and warnings-compliant, and checks for errors with open(). If
after doing this you are still seeing an error, feel free to post your
new program here for further assistance.

Paul Lalli

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-11-2005
[ Please provide context when replying to a message. ]

dario wrote:
> Gunnar Hjalmarsson wrote:
>> dario wrote:
>>> How do I make this work!!!

>>
>> <fragmentary code snipped>
>>
>> Please post a _short_ but _complete_ program that illustrates the
>> problem you are having, just as is explained in the posting guidelines
>> for this group.
>> http://mail.augustmail.com/~tadmc/cl...uidelines.html

>
> Sorry about the previuos post, i hope this is better!
> I want to match regex stored in a file to a text in a variable $head_. It
> works in windows, but not on linux.
> Thanks!
> Dario
>
> Content of a file rule.spam is :
> new_1 head Subject: .*\.\.
>
> Code is:
>
> $head_ ="Subject: Get cheap v i a g r a ..... ";
>
> open (NWRULE, "<rule.spam");
> @new_rule=<NWRULE>;
> close (NWRULE);
>
> foreach $rule(@new_rule)
> {
> if($rule =~ /(\S+) (\S+) ([^\n]+)/)
> {
> $new_id=$1;
> $dio=$2;
> $reg=$3;
> }
> if($head_ =~ m/$reg/)
> {
> print "something\n";# it doesn't match
> }
> }


That's still not a complete program that people can copy, paste and run
as is suggested in the posting guidelines. The below code is (I
think...). Note: strictures and warnings enabled; input data provided
via the __DATA__ token.

OTOH, the below program prints the expected result, so you wouldn't have
needed to post it. But if you had written it, you could have concluded
that what's probably causing your program to fail is that the open()
statement fails. Applying one of 'the golden rules', i.e. checking the
return value of open(), would likely have told you that as well.

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

my $head_ ="Subject: Get cheap v i a g r a ..... ";

while ( my $rule = <DATA> ) {
my ($new_id, $dio, $reg);
if ( $rule =~ /(\S+) (\S+) ([^\n]+)/ ) {
$new_id=$1;
$dio=$2;
$reg=$3;
}
if ( $head_ =~ m/$reg/ ) {
print "something\n";
}
}

__DATA__
new_1 head Subject: .*\.\.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      08-11-2005

dario wrote:

> Content of a file rule.spam is :
> new_1 head Subject: .*\.\.
>
> Code is:
>
> $head_ ="Subject: Get cheap v i a g r a ..... ";
>
> open (NWRULE, "<rule.spam");
> @new_rule=<NWRULE>;
> close (NWRULE);
>
> foreach $rule(@new_rule)
> {
> if($rule =~ /(\S+) (\S+) ([^\n]+)/)
> {
> $new_id=$1;
> $dio=$2;
> $reg=$3;
> }
> if($head_ =~ m/$reg/)
> {
> print "something\n";# it doesn't match
> }
> }


dario, I ran this in a DOS-box under windoze and the output was
"something".

dario, I presume that you aren't married to Perl and that
worshipping Perl isn't your religion. Therefore, you are
probably willing to switch to another language. Try Ruby.


$head_ ="Subject: Get cheap v i a g r a ..... "

rulelist = IO.readlines( 'rule.spam' )
rulelist.each { |rule|
rule =~ /(\S+) (\S+) (.+)/ or raise "Bad rule"
new_id, dio, reg = $~.captures
if $head_ =~ /#{reg}/
puts "Matched " + reg
end
}

 
Reply With Quote
 
Dario
Guest
Posts: n/a
 
      08-11-2005
Yes, I know that the code is less then perfect. I have to write it in perl.
I tried it on windows and it worked, but when I tried it on linux(perl
version 5.8.4) i didn't work.I think there is something to do with linux
platform but i don't know what. I'll try the things those guys suggested.
Thanks
"William James" <> wrote in message
news: ups.com...
>
> dario wrote:
>
>> Content of a file rule.spam is :
>> new_1 head Subject: .*\.\.
>>
>> Code is:
>>
>> $head_ ="Subject: Get cheap v i a g r a ..... ";
>>
>> open (NWRULE, "<rule.spam");
>> @new_rule=<NWRULE>;
>> close (NWRULE);
>>
>> foreach $rule(@new_rule)
>> {
>> if($rule =~ /(\S+) (\S+) ([^\n]+)/)
>> {
>> $new_id=$1;
>> $dio=$2;
>> $reg=$3;
>> }
>> if($head_ =~ m/$reg/)
>> {
>> print "something\n";# it doesn't match
>> }
>> }

>
> dario, I ran this in a DOS-box under windoze and the output was
> "something".
>
> dario, I presume that you aren't married to Perl and that
> worshipping Perl isn't your religion. Therefore, you are
> probably willing to switch to another language. Try Ruby.
>
>
> $head_ ="Subject: Get cheap v i a g r a ..... "
>
> rulelist = IO.readlines( 'rule.spam' )
> rulelist.each { |rule|
> rule =~ /(\S+) (\S+) (.+)/ or raise "Bad rule"
> new_id, dio, reg = $~.captures
> if $head_ =~ /#{reg}/
> puts "Matched " + reg
> end
> }
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57