Using variables in RegEx statements?

R

Rodney

I would like to do something like:

if ($SubIP_1 =~ /$SubIP[0]\.$SubIP[1]\.\d+\.\d+/)

But apparently, I can not use variables in the RegEx statement.

Can someone explain this to me?


Thanks,
 
U

Uri Guttman

R> I would like to do something like:
R> if ($SubIP_1 =~ /$SubIP[0]\.$SubIP[1]\.\d+\.\d+/)

R> But apparently, I can not use variables in the RegEx statement.

how did you derive that conclusion?

R> Can someone explain this to me?

can you show what data is in the variables? there is no way to debug it
given that one line of code.

uri
 
R

Rodney

Here's an example of my code so far... it simply doesn't work ????

##-- NOTE: I enter my own IP test in here for the test.
## I want to be able to match the first 2 parts of the IP address.

@IP_Rejects = ("123.123.123.123", "222.222.222.222");
$Remote_IP_Address = $ENV{REMOTE_ADDR};
my $KickOut = 0;
my $SubIP_1 = "";
my $SubIP = "";
my $ListTicker = 0;
my $ArraySize = @IP_Rejects;

while($ListTicker <= $ArraySize) {
$SubIP_1 = $IP_Rejects[$ListTicker];
@SubIP = split(/./, $Remote_IP_Address);

if ($SubIP_1 =~ /$SubIP[0]\.$SubIP[1]\.\d+\.\d+/) {
$KickOut = 1;
$ListTicker ++;
last;
}
$ListTicker ++;
}

if ($KickOut == 1) {
##-- send them a nice note.
}
 
R

Rodney

I found the problem....

I failed to use the \ in front of the period in split routine.

:)


Thanks everyone...
 
R

Rodney

Abigail" said:
:}
:} @IP_Rejects = ("123.123.123.123", "222.222.222.222");
:} $Remote_IP_Address = $ENV{REMOTE_ADDR};

What if this variable isn't set?
===============

If you mean the $Remote_IP_Address variable, how could that be?



:} @SubIP = split(/./, $Remote_IP_Address);

/./ matches *any* character, except newlines. So, @SupIP will turn
into an array loaded with empty strings.
==================

Actually, there is a period in there.... I found out that I also needed to
put a \ in front of that period, then it works.




:} if ($SubIP_1 =~ /$SubIP[0]\.$SubIP[1]\.\d+\.\d+/) {

That regex will expand to: /\.\.\d+\.\d+/, and unlikely to match anything.
======================

It seems to be working.... since I've included the \ in fron of the period
in the split routine.








--
Q. Why are there rolling green pastures
on the XP Desktop?

A. To keep the sheep happy.

swim away...
`·.¸¸.·´¯`·.¸¸.·´¯`·-> rodney
 
M

Martien Verbruggen

===============

If you mean the $Remote_IP_Address variable, how could that be?

That could be when $ENV{REMOTE_ADDRESS} isn't set. I just checked my
environment on four different machines here, and on none of them
REMOTE_ADDR is set.
==================

Actually, there is a period in there.... I found out that I also needed to
put a \ in front of that period, then it works.

Ok, so now you learned a trick.

Do you also understand _why_ you need to put a backslash in front of
that dot? Abigail already mentioned that a dot matches *any* character
(except newlines). What you need to do is try to understand what was
said there. First you need to read the entry for split() in perlfunc
to realise that that is called a regular expression. And then you need
to read about regular expressions in perlre (and probably perlretut).
Use the perldoc command to read documentation.
:} if ($SubIP_1 =~ /$SubIP[0]\.$SubIP[1]\.\d+\.\d+/) {

That regex will expand to: /\.\.\d+\.\d+/, and unlikely to match anything.
======================

It seems to be working.... since I've included the \ in fron of the period
in the split routine.

No. The regex that Abigail quoted was not working, since you complained
about it, with the original data. If you suddenly change the data then
the regex you posted would expand to something else than Abigail
quoted.

Be careful to read what people say, and respond appropriately.

Martien
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Here's an example of my code so far... it simply doesn't work ????

Let me guess: You're the kind of person who takes his car to the repair
shop and says, "My car doesn't work -- what's wrong with it????", right?

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP3AhxGPeouIeTNHoEQJfaQCeIq8q5lyng+up2grrfrgjflKBBdwAnAgC
JASt+EYr9+goZAQ6WlM+pk63
=1CX9
-----END PGP SIGNATURE-----
 
B

Barry Kimelman

[This followup was posted to comp.lang.perl.misc and a copy was sent to
the cited author.]

I would like to do something like:

if ($SubIP_1 =~ /$SubIP[0]\.$SubIP[1]\.\d+\.\d+/)

But apparently, I can not use variables in the RegEx statement.

Can someone explain this to me?


Thanks,

You must be making some sort of error in your code because Perl does
allow using variables in a RegEx as in the following example.

if ( $string =~ /${pattern_variable}/ )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top