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

Hi, I have a multiline string and I am trying to extract strings after matching a Key word . I have used look around assertion , But it matches only for the first occurrence in each line and skips the subsequent one's . Can any one please suggest a solution ? Below is my data and code .
data This is CentOs 6.5 version and CentOs 5.5 is not supported. The CentOS 6.0 has ability for LVM Snap backups where as CentOs 5.5 do +esn't.
Basically the word next to the keyword CentOs should be extracted , Below is the Regex that I am using . The multiline string is captured in a variable $x my ($y) = $x =~ /(?<=CentOs )(\d+\.\d+)/mg;

Replies are listed 'Best First'.
Re: How to find a string after matching a Keyword
by Athanasius (Archbishop) on Jan 08, 2015 at 02:33 UTC

    Hello pr33,

    I am not clear on exactly what you are wanting to do. However, it looks as though you want to find every number immediately following the string “CentOs”. In which case, you don’t need a look-around assertion; just proceed as follows:

    #! perl use strict; use warnings; use Data::Dump; $/ = undef; my $x = <DATA>; my @y = $x =~ / CentOs \s+ ( \d+ (?: \. \d+)? ) /mgix; dd \@y; __DATA__ data This is CentOs 6.5 version and CentOs 5.5 is not supported. The CentOS 6 has ability for LVM Snap backups where as CentOs 5.5 does +n't.

    Output:

    12:30 >perl 1117_SoPW.pl [6.5, 5.5, 6, 5.5] 12:30 >

    Note the addition of the /i modifier to allow “CentOS” to match as well. Also, I have assumed that a version number should match even if it lacks a decimal component.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: How to find a string after matching a Keyword
by LanX (Saint) on Jan 08, 2015 at 02:32 UTC
    This match returns all groups in list context but there is only one value you can grab with ($y) (a one element list)

    Try an @array instead or call it in a loop in scalar context (ie grabbing with $1)

    Furthermore it seems like a \s+ is missing where you test for whitespace.

    Cheers Rolf

    PS: Je suis Charlie!

Re: How to find a string after matching a string
by Anonymous Monk on Jan 08, 2015 at 07:52 UTC
    You don't need assertions to match a string next to a string
    #!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; my $data = q{ This is CentOs 6.5 version and CentOs 5.5 is not supported. The CentOS 6.0 has ability for LVM Snap backups where as CentOs 5.5 do +esn't.}; my @centos = $data =~ /CentOs\s+(\S+)/g; dd( \@centos ); __END__ [6.5, 5.5, 5.5]