Using variables in RegEx statements?

Discussion in 'Perl Misc' started by Rodney, Sep 22, 2003.

  1. Rodney

    Rodney Guest

    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, Sep 22, 2003
    #1
    1. Advertising

  2. Rodney

    Uri Guttman Guest

    >>>>> "R" == Rodney <> 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 ------ -------- 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 Guttman, Sep 22, 2003
    #2
    1. Advertising

  3. Rodney

    Rodney Guest

    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, Sep 22, 2003
    #3
  4. Rodney

    Rodney Guest

    I found the problem....

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

    :)


    Thanks everyone...

    --
    ....
    `·.¸¸.·´¯`·.¸¸.·´¯`·-> rodney
     
    Rodney, Sep 22, 2003
    #4
  5. Rodney

    Rodney Guest

    "Abigail" wrote:

    > :}
    > :} @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
     
    Rodney, Sep 22, 2003
    #5
  6. On Mon, 22 Sep 2003 18:29:02 -0400,
    Rodney <> wrote:
    > "Abigail" wrote:
    >
    >> :}
    >> :} @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?


    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.

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


    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
    --
    |
    Martien Verbruggen | Useful Statistic: 75% of the people make up
    Trading Post Australia | 3/4 of the population.
    |
     
    Martien Verbruggen, Sep 22, 2003
    #6
  7. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

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

    > 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-----
     
    Eric J. Roode, Sep 23, 2003
    #7
  8. [This followup was posted to comp.lang.perl.misc and a copy was sent to
    the cited author.]

    In article <aIJbb.10555$>,
    says...
    > 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}/ )
     
    Barry Kimelman, Sep 24, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    8
    Views:
    1,195
    mfmehdi
    Oct 20, 2006
  2. Harry George
    Replies:
    6
    Views:
    379
    Bart Nessux
    Feb 23, 2004
  3. Vince
    Replies:
    12
    Views:
    751
    Martin Gregorie
    Jan 21, 2008
  4. Replies:
    3
    Views:
    773
    Reedick, Andrew
    Jul 1, 2008
  5. John Crichton
    Replies:
    6
    Views:
    266
    John Crichton
    Jul 12, 2010
Loading...

Share This Page