Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

match a string in perl

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
ikon32
Asked:
ikon32
  • 4
  • 2
  • 2
2 Solutions
 
parparovCommented:
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
 
ikon32Author Commented:
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
 
schubachCommented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ikon32Author Commented:
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
 
schubachCommented:
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
 
ikon32Author Commented:
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
 
parparovCommented:
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
 
ikon32Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now