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

I have a question which is an extension of the solution that I got for my previous problem. Extracting specific text info from the config file ouput
I have a string as specified in the code below. I need a mechanisam to extract the authentication information.
For me the right authentication information are
1. authentication open <and/or followed by something>
eg1: authentication open network-eap
eg2: authentication open
2. authentication shared <and/or followed by something>
That means I should pick only the ones that start with authentication. Not the one starting with 'aaa' in the example below.
Also when I am looping I should strip of the part which I extracted from the original string.

Could some one tell me how to do it?
my $str = "aaa authentication login\x0d\n ssid Rich\x0d\n authentic +ation open\x0d\n authentication shared \x0d\n"; my $authentication; while ($str =~/authentication (\S+)/){ $authentication = $authentiction. $1. " "; } # At the end of the while loop # $authentication should have the value # $authentication = open shared
Any input provided will be much appreciated! Thanks.

Replies are listed 'Best First'.
Re: Extracting text from a string using regex
by holli (Abbot) on Dec 20, 2005 at 19:07 UTC
    Your code is close but has two problems. First you do not use the g modifier in your regex, so your code runs into an endless loop. Second, your have a typo in this line:
    $authentication .= $authentiction. $1. " ";
    That tells me you are using my but you are not using strict! Remember, always use strict.

    If you change that code to
    $authentication .= $authentication. $1. " ";
    all is fine.

    That all boils down to
    while ($str =~/authentication ?(\S+)?/g){ $authentication .= $authentiction. $1. " "; #more perlish }
    or even
    $authentication = join " ", $str =~ /authentication ?(\S+)?/g; #even + more perlish


    holli, /regexed monk/
Re: Extracting text from a string using regex
by GrandFather (Saint) on Dec 20, 2005 at 19:46 UTC

    Note that \r should generally be used for cr rather than \x0d.

    The following uses split to break the string into candidate sub strings, then uses a regex with a code evaluation expression in it to extract the text you want. A more conventional alternative is given too.

    use strict; use warnings; my $str = "aaa authentication login\r\n ssid Rich\r\n authentication o +pen\r\n authentication shared \r\n"; my $authentication = ''; my @candidates = split /[\r\n] +/, $str; /^authentication\s+(\w*)(?{$authentication.= $1 . ' '})/ for @candidat +es; print $authentication;

    Alternate extract code:

    for (@candidates) { $authentication.= $1 . ' ' if /^authentication\s+(\w*)/; }

    Either prints:

    open shared

    DWIM is Perl's answer to Gödel
Re: Extracting test from a string using regex
by davido (Cardinal) on Dec 20, 2005 at 18:49 UTC

    Something like this, if I understand the question correctly...

    m/^(authentication\s(shared|open)\s?(.+)?)/

    ...would be a reasonable choice for the regexp.

    $2 will contain shared or open. $3 will be defined and will contain whatever comes after shared or open, if something exists. That should get you going in the right direction.


    Dave