Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

I THOUGHT I'd finally understood regex

by JEDtoo (Novice)
on Jan 09, 2018 at 14:42 UTC ( #1206981=perlquestion: print w/replies, xml ) Need Help??
JEDtoo has asked for the wisdom of the Perl Monks concerning the following question:

Easy program to recognize local IP addresses within Internet IP addresses doesn't work correctly.
# local addresses are 192.168.x.x 10.x.x.x 127.0.0.1 172.16.x.x my @addrs = qw(61.177.172.64 110.53.183.252 195.154.47.194 110.53.183.252 190.96.252.188 221.194.47.243 10.1.1.1 100.1.1.1 192.168.0.1 127.0.0.1 172.16.0.1 172.17.0.1 ); $local_ip_match = "m/^192\.168\.|^10\.|^127\.0\.|^172\.16\./"; while (@addrs) { my $s = shift @addrs; if ($s =~ $local_ip_match) { print "got Local IP $s\n"; } else { print "got Internet IP $s\n"; } }
Results:
got Internet IP 61.177.172.64
got Internet IP 110.53.183.252
got Internet IP 195.154.47.194
got Internet IP 110.53.183.252
got Internet IP 190.96.252.188
got Internet IP 221.194.47.243
got Local IP 10.1.1.1 YES
got Local IP 100.1.1.1 NO NO - didn't I say 10 followed by a . ??
got Internet IP 192.168.0.1 NO NO - doesn't this match the first part?
got Local IP 127.0.0.1 Right
got Internet IP 172.16.0.1 NO NO -- I thought I had this fingered out?
got Internet IP 172.17.0.1

Replies are listed 'Best First'.
Re: I THOUGHT I'd finally understood genex
by Eily (Prior) on Jan 09, 2018 at 14:48 UTC

    $local_ip_match starts by the character m, so it will try to match something that contains an m. That's probably not what you want.

    The m in m/REGEX/ is an operator, so it should not be inside the string. You can try:
    $s =~ m/^192\.168\.|^10\.|^127\.0\.|^172\.16\./
    or if you want to store the regex in a variable:  $local_ip_match = qr/^192\.168\.|^10\.|^127\.0\.|^172\.16\./; see qr

    Who's genex?

      1) genex are either "Young folks" or a lisdexic spelling of regex - you choose
      2) using qr fixed my problem, which WASN'T the expression so changing it was NEVER going to work.THANK YOU!!
      BTW: The list of Internet IPs is a list of real addresses trying to log into my server as root or admin or pi right now. My REAL program blocks all further traffic from those addresses, unless they are local.

        The youngest genexers® are in their late 30s now with the bulk being in their 40s and some in their 50s; only be "young folks" in Okinawa. :P

      Who's genex?
      JEDtoo probably means regex.

      Update: ... and, BTW, has now fixed the title of the original post.

        And I was joking ;-). The original title was edited anyway :)

Re: I THOUGHT I'd finally understood genex
by wjw (Priest) on Jan 09, 2018 at 14:51 UTC
    You may find this CPAN module helpful in that you don't have to re-invent.... (Unless you want to re-invent, which is a good way to learn)

    Net::IP::Match::Regexp

    ...the majority is always wrong, and always the last to know about it...

    A solution is nothing more than a clearly stated problem...

      I THOUGHT I was doing something simple. I thought by using a string at the top for the expression I was making it easier to read. Sometimes I think too much.
      My thanks to everyone for the help!
        I thought by using a string at the top for the expression I was making it easier to read.

        Even without the confounding  m/ / pieces in the string, it's almost never a good idea to use either a single- or double-quoted string to define a regex if you can avoid it; many subtle pitfalls lurk there. Use the  qr// operator instead; see Regexp Quote-Like Operators in perlop.


        Give a man a fish:  <%-{-{-{-<

Re: I THOUGHT I'd finally understood regex
by FloydATC (Deacon) on Jan 09, 2018 at 15:44 UTC

    Keep in mind that 172.16 is a /12 block and not /16 as your code would seem to suggest.

    The quick and dirty fix is to include 172.17, 127.18 etc up to 172.31 but I would definitely use the suggested CPAN module instead if I were you.

    -- FloydATC

    I got 99 problems, most of them have to do with printers.

      Good point. Yes, I HAD forgotten that. Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1206981]
Approved by Eily
Front-paged by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2018-05-24 02:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?