Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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 172.16.x.x my @addrs = qw( ); $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"; } }
got Internet IP
got Internet IP
got Internet IP
got Internet IP
got Internet IP
got Internet IP
got Local IP YES
got Local IP NO NO - didn't I say 10 followed by a . ??
got Internet IP NO NO - doesn't this match the first part?
got Local IP Right
got Internet IP NO NO -- I thought I had this fingered out?
got Internet IP

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)


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

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1206981]
Approved by Eily
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (1)
As of 2018-08-18 14:09 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (185 votes). Check out past polls.