Loading ...
Sorry, an error occurred while loading the content.

Question regarding regexp

Expand Messages
  • leifedvin
    Hi, this script should check whether a string contains an IP address or not. When I run it everything seems OK, but I miss the values of $ip and $string in the
    Message 1 of 3 , May 2, 2004
    • 0 Attachment
      Hi,

      this script should check whether a string contains an IP address
      or not. When I run it everything seems OK, but I miss the values
      of $ip and $string in the output. Note the empty space in the
      beginning of the line. Where is the flaw in my code?
      Thanks in advance for your help.

      Regards,
      Leif Edvin

      ---Script---
      #!/usr/bin/perl
      #ip_address.plx
      use warnings;
      use strict;

      my ($ip, $string);
      while (<STDIN>){
      chomp ($string = <STDIN>);
      if(/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/){
      $ip = $1;
      print "$ip contains an valid IP address.\n";
      } else {
      print "$string is not an IP address.\n";
      }
      }
      ---End of script---

      ---Output---
      127.0.0.1 (Pressed Enter twice)

      contains an valid IP address.
      jabba dabba (Pressed Enter twice)

      is not an IP address.
    • Allan Dystrup
      Hi Leif Try: ____________________________________________________________________ #!/usr/bin/perl #ip_address.plx use warnings; use strict; my ($ip, $string);
      Message 2 of 3 , May 2, 2004
      • 0 Attachment
        Hi Leif

        Try:
        ____________________________________________________________________
        #!/usr/bin/perl
        #ip_address.plx
        use warnings;
        use strict;

        my ($ip, $string);
        while (chomp ($string=<STDIN>) ne "" ) {
        if( $string =~/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/ ){
        $ip = $1;
        warn "[$ip] contains an valid IP address.\n";
        } else {
        warn "[$string] is not an IP address.\n";
        }
        }
        ____________________________________________________________________

        Allan
        - "never let the blind lead the blind..."



        --- In perl-beginner@yahoogroups.com, "leifedvin" <leifedvin@y...>
        wrote:
        > Hi,
        >
        > this script should check whether a string contains an IP address
        > or not. When I run it everything seems OK, but I miss the values
        > of $ip and $string in the output. Note the empty space in the
        > beginning of the line. Where is the flaw in my code?
        > Thanks in advance for your help.
        >
        > Regards,
        > Leif Edvin
        >
        > ---Script---
        > #!/usr/bin/perl
        > #ip_address.plx
        > use warnings;
        > use strict;
        >
        > my ($ip, $string);
        > while (<STDIN>){
        > chomp ($string = <STDIN>);
        > if(/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/){
        > $ip = $1;
        > print "$ip contains an valid IP address.\n";
        > } else {
        > print "$string is not an IP address.\n";
        > }
        > }
        > ---End of script---
        >
        > ---Output---
        > 127.0.0.1 (Pressed Enter twice)
        >
        > contains an valid IP address.
        > jabba dabba (Pressed Enter twice)
        >
        > is not an IP address.
      • Allan Dystrup
        Btw Leif, Apart from the syntactic issues dealt with below, there is the semantic issue of a well formed IP address . see for instance : Quote :
        Message 3 of 3 , May 2, 2004
        • 0 Attachment
          Btw Leif,

          Apart from the syntactic issues dealt with below, there is the
          semantic issue of a "well formed IP address". see for instance :

          Quote : http://www.regular-expressions.info/examples.html
          IP Addresses

          Matching an IP address is another good example of a trade-off between
          regex complexity and exactness. \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
          \b will match any IP address just fine, but will also match
          999.999.999.999 as if it were a valid IP address. Whether this is a
          problem depends on the files or data you intend to apply the regex
          to.

          To restrict all 4 numbers in the IP address to 0..255, you can use
          this complex beast: \b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]
          |2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
          \.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b (everything on a single
          line). The long regex stores each of the 4 numbers of the IP address
          into a capturing group. You can use these groups to further process
          the IP number.

          If you don't need access to the individual numbers, you can shorten
          the regex with a quantifier to: \b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9]
          [0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b. Similarly,
          you can shorten the quick regex to \b(?:\d{1,3}\.){3}\d{1,3}\b

          / allan

          --- In perl-beginner@yahoogroups.com, "Allan Dystrup"
          <allan_dystrup@y...> wrote:
          >
          > Hi Leif
          >
          > Try:
          > ____________________________________________________________________
          > #!/usr/bin/perl
          > #ip_address.plx
          > use warnings;
          > use strict;
          >
          > my ($ip, $string);
          > while (chomp ($string=<STDIN>) ne "" ) {
          > if( $string =~/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/ ){
          > $ip = $1;
          > warn "[$ip] contains an valid IP address.\n";
          > } else {
          > warn "[$string] is not an IP address.\n";
          > }
          > }
          > ____________________________________________________________________
          >
          > Allan
          > - "never let the blind lead the blind..."
          >
          >
          >
          > --- In perl-beginner@yahoogroups.com, "leifedvin" <leifedvin@y...>
          > wrote:
          > > Hi,
          > >
          > > this script should check whether a string contains an IP address
          > > or not. When I run it everything seems OK, but I miss the values
          > > of $ip and $string in the output. Note the empty space in the
          > > beginning of the line. Where is the flaw in my code?
          > > Thanks in advance for your help.
          > >
          > > Regards,
          > > Leif Edvin
          > >
          > > ---Script---
          > > #!/usr/bin/perl
          > > #ip_address.plx
          > > use warnings;
          > > use strict;
          > >
          > > my ($ip, $string);
          > > while (<STDIN>){
          > > chomp ($string = <STDIN>);
          > > if(/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/){
          > > $ip = $1;
          > > print "$ip contains an valid IP address.\n";
          > > } else {
          > > print "$string is not an IP address.\n";
          > > }
          > > }
          > > ---End of script---
          > >
          > > ---Output---
          > > 127.0.0.1 (Pressed Enter twice)
          > >
          > > contains an valid IP address.
          > > jabba dabba (Pressed Enter twice)
          > >
          > > is not an IP address.
        Your message has been successfully submitted and would be delivered to recipients shortly.