?
Solved

match a string in perl

Posted on 2011-09-21
8
Medium Priority
?
368 Views
Last Modified: 2012-06-27
Hi, in the description I want to match words from a log file. In the code I have it match only when the bann word is found by itself and not if somethig else is there with it.
How can this match in all cases?
the bannwords file looks lilke this:
--------------------------------------------
word1
word2
word3
word4
-----------------------------------------------
thanks  
if($description) {
foreach $bannedword (@badwords) {
chomp $bannedword;
if ($bannedword =~ /$description/i) {  print "Location: $gtoUrl\n\n"; exit; }
        }
}

Open in new window

0
Comment
Question by:ikon32
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 9

Accepted Solution

by:
parparov earned 1000 total points
ID: 36573678
You are doing it the opposite way.

The correct matching should be:

if ($description =~ /$bannedword/i) {  print "Location: $gtoUrl\n\n"; exit; }

Open in new window


Please note that in this case even larger _words_ that contain the banned word (like mass, in case ass is banned) will be matched. If you want to avoid this case, do
if ($description =~ /\b$bannedword\b/i) {  print "Location: $gtoUrl\n\n"; exit; }

Open in new window

0
 

Author Comment

by:ikon32
ID: 36573746
Thanks for the reply, I have try this before, actualy I have try many things.
 like this:
if ($description =~ /\b$bannedword\b/i) {  print "Location: $gtoUrl\n\n"; exit; }

it dosn't match any words I realy dont know why? it seems it sould though...
and the only way it is working (partialy) is the way in my first post. Any ideas?
0
 
LVL 4

Assisted Solution

by:schubach
schubach earned 1000 total points
ID: 36574052
This has been tested and works.  You can change the print "is bad word" to a Location call.
#!/usr/bin/perl

use strict;

my @badwords = ("word1","word2","word3","word4");
my @descriptions = ("hi word1","word12 acceptable","fine","word3");
foreach my $descr(@descriptions) {
  if (isBad($descr)) {
    print "$descr is bad word\n";
  } else {
    print "$descr is good word\n";
  }
}

sub isBad {
  my $description = shift;
  foreach my $bannedword (@badwords) {
    chomp $bannedword;
    if ($description =~ /\b$bannedword\b/i)
    {
      return 1;
    }
  }
  return 0;
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ikon32
ID: 36574572
I like this but how can I emplement it?
I try to change it a litle because $description comes from a form so does a $url so I try to bann based on $description and how if I dont use (use strict?) the reason is this is an old long script it will take long time...
The way I put it in doesn't work. Propable my fault! Can you pls check the code and see where Iam wrong?
here is the code until this part:  
#!/usr/bin/perl
use CGI;
require "setup.cgi";
$q = new CGI;
$url= $q->param('url');
$description = $q->param('title');
$page_name = $q->param('page');
$spec_value = $q->param('spec');

$Http_referer    =   $ENV{'HTTP_REFERER'};  
$Remote_host     =   $ENV{'REMOTE_HOST'};  
$Remote_address  =   $ENV{'REMOTE_ADDR'};  
$Remote_identity =   $ENV{'REMOTE_IDENT'}; 
$Script_name     =   $ENV{'SCRIPT_NAME'};  
$Server_name     =   $ENV{'SERVER_NAME'};

$gtoUrl="http://www.site.com";
$referer = "www.www.site.com";

if ($Http_referer =~ m/$referer/i){ &bypass; }
else  { print "Location: $gtoUrl\n\n"; exit;}
sub bypass {   }

open(SSS, "$banned") || die "Can't open FILE: $banned\n";
flock (SSS, 2);
@banneded = <SSS>;
flock (SSS, 8);
close(SSS);

##  take out banned domains this works great
if($url) {
foreach $ban (@banneded) {
chomp $ban;
if ($ban =~ /$url/)   {  &outer; }
           }
}
#   take out banned description 
open(BANSSS, "$banned_words") || die "Can't open FILE: words\n";
flock (BANSSS, 2);
@badwords = <BANSSS>;
flock (BANSSS, 8);
close(BANSSS);

 @descriptions = ("$description"); ## changed this
foreach  $descr(@descriptions) {
  if (isBad($descr)) {
    print "Location: $gtoUrl\n\n"; exit; 
  } else {
    &contdown;
  }
}
sub isBad {
  my $description = shift;
  foreach  $bannedword (@badwords) {
    chomp $bannedword;
    if ($description =~ /\b$bannedword\b/i)
    {
      return 1;
    }
  }
  return 0;
}
sub contdown { }
####################################

Open in new window

0
 
LVL 4

Expert Comment

by:schubach
ID: 36574837
The line
open(BANSSS, "$banned_words") || die "Can't open FILE: words\n";

Open in new window

should die, because $banned_words is not defined, so it is trying to open an unnamed file.  If it's not dying there, then my guess is that your code is exiting on line 21. Try changing line 21 to:
else  { print "Content-type: text/html\n\n".__LINE__; exit;}

Open in new window

so you can see if it is really bailing where you think it is.  

Since this is old code, it is also possible that the die is somehow being intercepted, in which case you might not see it.  Something in setup.cgi might be doing this.  Try fixing the file name and see if that solves your problem.
0
 

Author Comment

by:ikon32
ID: 36575481
I naild it . But I dont understand why is it not working with a single line like this?
if ($description =~ /\b$bannedword\b/i) {  print "Location: $gtoUrl\n\n"; exit; } as parparov sujested before and go through the line $description,
and works with a nested foreach after braking down the variable like this:
if($description) {
foreach $ban_word (@banneded_words) {

@mydescriptions = split/ /,$description;
 foreach $descr (@mydescriptions) {
if ($ban_word =~ /\b$descr\b/i) { print "Content-type: text/html\n\n"; print "$ban_word === found here"; exit;  }
}
}
}
any ideas?
thanks

0
 
LVL 9

Expert Comment

by:parparov
ID: 36575498
You keep changing the code; you may want to show up a sample of banned word and a description that do not work, or better as full am example, as possible
0
 

Author Comment

by:ikon32
ID: 36575812
A sample of bad words in the file:
------------------ just one word in each line
cialis
Ativan
xanax
porn
pharmacy
nude
pharm
blah
blah
------------------------------------------------
The $description is comming from a form <input text....>. It contains anything that somone can write in it.

I did not change any code! exept if you mean the names of  $variables.
The hart of my problem was in this line:
if ($bannedword =~ /$description/i) {  print "Location: $gtoUrl\n\n"; exit; }
(now if this is print... or go to location it doesn't mean anything as long as the code arive there)

I spend 2 days searching on the internet before I come here for help and your soloution according to what I know and what I had red on the internet it sould work and it is correct. It is just not working in this case in my script and I try to understand why?

Breaking down the variable and then compare is working, but why is it not working with your solution is what I am trying to undertand.
A sample of my script as it is exactly writen I already provided.  

Any way It would be nice to know and understand why your solution is not working in this case even though I think is correct and poins rewarded.
Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question