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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Regular Expressions:
variables in regular expressions



Jul 6, 2001, 10:05 AM

Post #1 of 4 (16773 views)
variables in regular expressions Can't Post

I am trying to use 2 variables in my regex and I can't get it to work. I can never get it to evaluate to true. Here's a code snippet ------>


%hash = (data => "");

$entry = "";

if ($entry =~ m/%hash->{'data'}+/) {

print "Match\!\n";


Can someone please tell me what I'm doing wrong?

thanks for your help


Jul 6, 2001, 10:45 AM

Post #2 of 4 (16769 views)
Re: variables in regular expressions [In reply to] Can't Post



if ($entry =~ m/%hash->{'data'}+/) {


if ($entry =~ m/$hash{'data'}+/) {

I Hope this helps...

perl programmers don't die.. they just start writing a new script.


Jul 6, 2001, 12:15 PM

Post #3 of 4 (16767 views)
Re: variables in regular expressions [In reply to] Can't Post

I tried that and now I get a match every time,,,whethere the hash is included in my script or not. I am running a program called mrtg to monitor our routers which outputs some html files for each router into a predefined directory. I want to do a couple of things with the html files. The html files are named the same - is a good example. I want to parse the ip address out of the filename and compare it to a record in a table in the mysql database. If it's found in the database, I want to query and see if the ip( and interface(se0.1) exist in the interfaces table. So basically I need to setup the query, execute it, read the path where the file should be, if it's there parse the filename and put the ip/interface into the interfaces table. That shouldn't be too hard with perl, right? :-)

thanks for all of your help


Jul 6, 2001, 12:58 PM

Post #4 of 4 (16766 views)
Re: variables in regular expressions [In reply to] Can't Post


I've commented the code, so it shouldn't be hard to understand.

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

# Use the following to see what the
# regex engine is actually doing...
use re 'debug';

my $entry = "";

my %hash = (data => "");

# All strings that are about to be
# interpolated into a regex should
# be quotemeta'd before. Otherwise,
# the dots will match any character!
$_ = quotemeta foreach values %hash;

# Now, we want to check if $entry
# begins with $hash{data}...
if( $entry =~ /^$hash{data}/ ) {
print "Match!\n";

If you have any specific questions, feel free to ask. Hope this helps.

-- Marcus


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

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