Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a list of pc names and ip addresses and some pc names have been seen with more than one IP as they log into our network. I want to strip off all the extra ips and just pull back the first one.
10.64.160.11, 10.64.161.166, 10.64.184.111, 10.64.184.29
So in the post above, I only want to get 10.64.160.11.

Replies are listed 'Best First'.
Re: Regex to take an ip address before a comma
by moritz (Cardinal) on Nov 25, 2008 at 15:29 UTC
    This can be as simple as splitting on the first comma and taking the first chunk.

    Regexp::Common::net also provides regexes for parsing IPv4 addresses.

Re: Regex to take an ip address before a comma
by ikegami (Patriarch) on Nov 25, 2008 at 15:44 UTC

    Assigns everything up to the first comma to $first:

    my ($first) = $str =~ /(.*?),/;

    Not as efficient, but possibly more readable:

    my ($first) = split(/,\s*/, $str);

    Update: D'oh! Fixed bug mentioned in reply.

      Assigns everything up to the first comma to $first
      $ perl -wle 'my $str = "a, b,"; my ($first) = $str =~ /(.*),/; print $ +first' a, b
      Up to the last comma, actually. If you want "up to the first comma" either use ([^,]*), or (.*?),

      I think the split() solution is more efficient than the regex solution.

      Tested with Perl 5.8.8 (ActiveState on WinXP)

      #!/usr/bin/perl use strict; use warnings; use Benchmark qw( cmpthese ); my $str = ( ('X'x15) . ',' ) x 3; cmpthese( -1, { # using regex 's///' => sub { my ( $first ) = $str =~ m/(.*?),/; }, # using split without limit 'split_unl' => sub { my ( $first ) = split( m/,/, $str ); }, # using split with limit 'split_lim' => sub { my ( $first ) = split( m/,/, $str, 2 ); }, # using split with limit and slice for scalar assignment 'split_lim_sca' => sub { my $first = ( split( m/,/, $str, 2 ) )[0]; }, } )

      my result:

      Rate s/// split_lim split_unl split +_lim_sca s/// 577066/s -- -13% -13% + -15% split_lim 663032/s 15% -- -0% + -2% split_unl 663629/s 15% 0% -- + -2% split_lim_sca 679348/s 18% 2% 2% + --

      Or did I miss something?

        and here's my result with a perl 5.10 (cygwin on same WinXP)

        Rate s/// split_lim split_unl split +_lim_sca s/// 288797/s -- -12% -12% + -15% split_lim 326805/s 13% -- -1% + -4% split_unl 330050/s 14% 1% -- + -3% split_lim_sca 339541/s 18% 4% 3% + --
Re: Regex to take an ip address before a comma
by linuxer (Curate) on Nov 25, 2008 at 16:21 UTC

    I would use

    my $first = ( split /,\s*/, $line, 2 )[0]

    to extract the first element.

    I would do the test if $first is an ip address afterwards.

    perldoc -f split
Re: Regex to take an ip address before a comma
by oeuftete (Monk) on Nov 25, 2008 at 15:44 UTC

    What moritz said. This would be a start, which you might need to tweak based on any unmentioned requirements:

    use Regexp::Common qw(net); while (<>) { my ($keeper) = $_ =~ / ($RE{net}{IPv4}) # Capture the IP , # comma /x; printf "%s\n", $keeper if $keeper; }
Re: Regex to take an ip address before a comma
by brsaravan (Scribe) on Nov 26, 2008 at 06:11 UTC
    You can try this
    $strout = (split(',',$line))[0];
Re: Regex to take an ip address before a comma
by Lawliet (Curate) on Nov 25, 2008 at 20:58 UTC

    Why has no one suggested that OP have a look at How do I post a question effectively? yet?

    Message to OP: We will never usually not write your code for you. Please show some effort first.

    I'm so adjective, I verb nouns!

    chomp; # nom nom nom