Professional Web Applications Themes

Using variables in RegEx statements? - PERL Miscellaneous

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, -- .... `..`..`-> rodney...

  1. #1

    Default Using variables in RegEx statements?

    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,
    --
    ....
    `..`..`-> rodney


    Rodney Guest

  2. #2

    Default Re: Using variables in RegEx statements?

    Rodney (net) wrote on MMMDCLXXIV September
    MCMXCIII in <URL:news:aIJbb.10555$bellsouth.net>:
    !! 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?


    You must be doing something wrong, because I can:

    #!/usr/bin/perl

    use strict;
    use warnings;
    no warnings qw /syntax/;

    my SubIP = qw /192 168/;

    my $SubIP_1 = '192.168.0.0';

    if ($SubIP_1 =~ /$SubIP[0]\.$SubIP[1]\.\d+\.\d+/) {
    print "Found!\n";
    }
    __END__
    Found!



    Abigail
    --
    # Count the number of lines; code doesn't match \w. Linux specific.
    ()=<>;$!=$=;($:,$,,$;,$")=$!=~/.(.)..(.)(.)..(.)/;
    $;++;$*++;$;++;$*++;$;++;`$:$,$;$" $. >&$*`;
    Abigail Guest

  3. #3

    Default Re: Using variables in RegEx statements?

    >>>>> "R" == Rodney <net> writes:

    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

    --
    Uri Guttman ------ com -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
    Uri Guest

  4. #4

    Default Re: Using variables in RegEx statements?

    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.
    }






    --
    ....
    `..`..`-> rodney


    Rodney Guest

  5. #5

    Default Re: Using variables in RegEx statements?

    Rodney (net) wrote on MMMDCLXXIV September
    MCMXCIII in <URL:news:YmKbb.12719$bellsouth.net>:
    :} 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};

    What if this variable isn't set?

    :} 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);

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

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

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

    :} $KickOut = 1;
    :} $ListTicker ++;
    :} last;
    :} }
    :} $ListTicker ++;
    :} }


    Abigail
    --
    INIT {print "Perl " }
    BEGIN {print "Just " }
    CHECK {print "another "}
    END {print "Hacker\n"}
    Abigail Guest

  6. #6

    Default Re: Using variables in RegEx statements?

    I found the problem....

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

    :)


    Thanks everyone...

    --
    ....
    `..`..`-> rodney


    Rodney Guest

  7. #7

    Default Re: Using variables in RegEx statements?

    "Abigail" wrote:
     
    ===============

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



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

    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.




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

    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


    Rodney Guest

  8. #8

    Default Re: Using variables in RegEx statements?

    On Mon, 22 Sep 2003 18:29:02 -0400,
    Rodney <net> wrote: 
    >===============
    >
    > If you mean the $Remote_IP_Address variable, how could that be?[/ref]

    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.[/ref]

    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 doentation.
     
    >======================
    >
    > It seems to be working.... since I've included the \ in fron of the period
    > in the split routine.[/ref]

    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
    --
    |
    Martien Verbruggen | Useful Statistic: 75% of the people make up
    Trading Post Australia | 3/4 of the population.
    |
    Martien Guest

  9. #9

    Default Re: Using variables in RegEx statements?

    Rodney wrote:
     
     

    (snipped a lot of code)

    You are looking to match the first two octets.

    Your code is very complex and invokes a lot more actions
    than needed. You have no need, based on your parameters
    written in your code, to check the last two octets.

    However, you are probably blocking more hosts than you want.
    If you are looking to block a specific host, matching the
    first three octets would be more accurate but not foolproof;
    you will still block more hosts than you probably realize.

    #!perl

    IP_Rejects = qw (123.123 222.222);
    $ENV{REMOTE_ADDR} = "222.222.222.222";

    for (IP_Rejects)
    {
    if (index ($ENV{REMOTE_ADDR}, $_) == 0)
    { print "This One Is Rejected."; last; }
    }


    That is all you need to attain your task as exemplified
    by your code example.


    Purl Gurl
    --
    Award Winning Rock N Roll Midi Files!
    http://www.purlgurl.net/~callgirl/rockmusi.html
    Performed And Recorded Live Here At Home.
    Godzilla Guest

  10. #10

    Default Re: Using variables in RegEx statements?

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

    "Rodney" <net> wrote in news:YmKbb.12719$iO.1221
    bignews5.bellsouth.net:
     

    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+up2grrfrgjfl KBBdwAnAgC
    JASt+EYr9+goZAQ6WlM+pk63
    =1CX9
    -----END PGP SIGNATURE-----
    Eric Guest

  11. #11

    Default Re: Using variables in RegEx statements?

    "Eric J. Roode" <net> writes: 
    >
    > 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?[/ref]

    Which is a completely rational thing to do when you're paying a
    professional to diagnose and fix the problem. Posting to clpm, OTOH,
    is almost exactly the opposite case. :)

    -=Eric
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
    Eric Guest

  12. #12

    Default Re: Using variables in RegEx statements?

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

    In article <aIJbb.10555$bellsouth.net>,
    net says... 

    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}/ )

    Barry Guest

Similar Threads

  1. If Statements????
    By ccarterca in forum Macromedia Dynamic HTML
    Replies: 0
    Last Post: October 17th, 12:34 PM
  2. If statements in PHP?
    By barbedwire103 in forum Dreamweaver AppDev
    Replies: 1
    Last Post: April 11th, 11:24 AM
  3. echo statements
    By Jay Fitzgerald in forum PHP Development
    Replies: 2
    Last Post: July 19th, 12:36 AM
  4. IF, THEN, ELSE statements
    By Jeremy in forum Microsoft SQL / MS SQL Server
    Replies: 3
    Last Post: July 15th, 04:45 AM
  5. If/else statements - help.
    By Chris in forum PHP Development
    Replies: 1
    Last Post: June 28th, 01:55 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139