Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7788893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:07:21+00:00 2026-06-01T21:07:21+00:00

This is a follow-up from Perl regular expression to match an IP address .

  • 0

This is a follow-up from Perl regular expression to match an IP address. I wanted to show how to solve the problem correctly, but ran into an unexpected behaviour.

use 5.010;
use strictures;
use Data::Munge qw(list2re);
use Regexp::IPv6 qw($IPv6_re);
use Regexp::Common qw(net);

our $port_re = list2re 0..65535;

sub ip_port_from_netloc {
    my ($sentence) = @_;
    return $sentence =~ /
        (                   # capture either
          (?<= \[ )
            $IPv6_re        #  IPv6 address without brackets
          (?=  \] )
        |                   # or
            $RE{net}{IPv4}  #  IPv4 address
        )
        :                   # colon sep. host from port
        ($port_re)          #   capture port
    /msx;
}

my ($ip, $port);
($ip, $port) = ip_port_from_netloc 'The netloc is 216.108.225.236:60099';
say $ip;
($ip, $port) = ip_port_from_netloc 'The netloc is [fe80::226:5eff:fe1e:dfbe]:60099';
say $ip;

The second match fails. use re 'debugcolor' reveals that :($port_re) already matches :5 within the IPv6 address. This surprises me because I did not switch off greediness with a ?. I expected it to gobble up everything up to the ], only then match against the separating colon and what follows after.

Why does this happen, and what’s the remedy?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:07:22+00:00Added an answer on June 1, 2026 at 9:07 pm

    Greed would only come into play if one of your atoms has a choice in how much it can match (i.e. if you used *, +, ? or {n,m}). This is not a greediness issue.

    The problem is that the regex will only match an IPv6 address if it’s immediately followed by both “]” and by “:“. That can’t possibly happen.

    You could use two different matches, or you could use something like the following:

    my $port_re = list2re 0..65535;
    my $IPv4_re = $RE{net}{IPv4};
    
    sub ip_port_from_netloc {
        my ($sentence) = @_;
        return if $sentence !~ /
            (?: \[ ( $IPv6_re ) \]
            |      ( $IPv4_re )
            )
            : ($port_re)
        /msx;
    
        return ($1 // $2, $3);
    }
    

    Maybe this is a bit cleaner?

    my $port_re = list2re 0..65535;
    my $IPv4_re = $RE{net}{IPv4};
    
    sub ip_port_from_netloc {
        my ($sentence) = @_;
        return if $sentence !~ /
            (?: \[ (?<addr> $IPv6_re ) \]
            |      (?<addr> $IPv4_re )
            )
            : (?<port> $port_re )
        /msx;
    
        return ( $+{addr}, $+{port} );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow up from a question of mine that was just answered
This is a follow on from my previous question although this is about something
This is a follow-on from this question, in which I was trying to suppress
I am trying to follow this example (from p137 of Rob Pickering's Foundations of
This is a follow-on question from the one I asked here . Can constraints
This question is a follow on from this one ... I am binding to
This problem follows on from a previous question . When I run the following
After asking this perl newbie question , I have a perl newbie follow-up. I
(This is a follow-up from this previous question ). I was able to successfully
This is a follow-on from a previous question, in the implementation, I have two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.