Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Match Ip Address on cmd line

by NetWallah (Canon)
on Sep 09, 2011 at 05:13 UTC ( [id://925011]=note: print w/replies, xml ) Need Help??


in reply to Match Ip Address on cmd line

This 'quick and dirty' regex will work reasonably well for "reasonable" data:
perl -ne 'print qq|$1\n| if m/^\s*((?:\d{1,3}\.){3}\d{1,3})\s*$/'
It is tolerant of leading and trailing white-space, but does not check for octets being under 255, or being all zero at the first octet. There are "standard regex" Regexp::Common modules that do a more thorough check for IPv4 validity.

            "XML is like violence: if it doesn't solve your problem, use more."

Replies are listed 'Best First'.
Re^2: Match Ip Address on cmd line
by sans-clue (Beadle) on Sep 09, 2011 at 14:03 UTC
    thanks so much, does the trick. what does qq mean ?

    Also, yes I know this isn't a code writing service, but I could not find an 'if m/...' example in a command line, plus I would have never come up with 2 |'s

      The things you ask about are partly about style choices.

      Equivalent:

      do_something() if <condition>; if (<condition>) { do_something(); }

      Equivalent:

      m[regex] m|regex| m/regex/ /regex/

      Equivalent:

      "string-with-expanded-$variables" qq[string-with-expanded-$variables] qq/string-with-expanded-$variables/
      Using qq// is common on command lines, since the shell itself may require quotes or double-quotes.

      Equivalent:

      'string-expanding-no-variables' q[string-expanding-no-variables] q/string-expanding-no-variables/
      All of this can be found in perldoc perlsyn and perldoc perlop.

      --
      [ e d @ h a l l e y . c c ]

        veddy nice I like. thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://925011]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-19 08:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found