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

Seeking help on how to extract portion,

..\../system-build/ali/text/makefile

from the pattern given below

make -r -f ..\../tools/cell/makefile cell.lib

I tried regex given below, but did not work; could any one tell me what is wrong with this ?

/(.*)\s\.\.\\(.*)\/makefile/si

TIA

Then

  • Comment on using regular expression to extract subset from a line

Replies are listed 'Best First'.
Re: using regular expression to extract subset from a line
by DamnDirtyApe (Curate) on Dec 30, 2004 at 19:54 UTC

    I'll leave the analysis of what you've got to someone who's better at regexps than I, but if you're simply looking to extract the argument from a space-delimited string that ends in "makefile", try something simpler like this:

    #! /usr/bin/perl -w use strict; my $old_str = 'make -r -f ..\../tools/cell/makefile cell.lib'; my $new_str = ( $old_str =~ /\s+(\S*makefile)/ )[0]; print "New string: $new_str$/"; __END__

    HTH

    Update: You could do this with split and grep also:

    my $new_str = ( grep { /makefile$/ } split /\s+/, $old_str )[0];

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: using regular expression to extract subset from a line
by Eimi Metamorphoumai (Deacon) on Dec 30, 2004 at 19:57 UTC
    What exactly do you know about the part you want to extract? Here's a way that's similar to yours
    /.*\s(\.\.\\.*\/makefile)/i;
    which will put the matched text in $1. But personally, I'd probably use something like
    /-f\s*(\S+)/;
    to find whatever text is following the -f switch.
Re: using regular expression to extract subset from a line
by johnnywang (Priest) on Dec 30, 2004 at 20:21 UTC
    It looks like you want everything after "-f" up to "makefile", how about the following:
    use strict; while(<DATA>){ print m|-f\s+(\S+makefile)|i; } __DATA__ make -r -f ..\../tools/cell/makefile cell.lib
    Depending on how you're testing your regex, your problem could be whether you're escaping the "\" in your string.
Re: using regular expression to extract subset from a line
by perlsen (Chaplain) on Dec 31, 2004 at 04:16 UTC

    I think ur requirement is extract upto the makefile
    if u wish u can modify ur regexp to as

    inputs: ********* #$str='make -r -f ..\../tools/cell/makefile cell.lib'; $str='make -r -f ..\../system-build/ali/text/makefile cell.lib'; regexp: ********** $str=~/(.*)\s(\.\.\\(.*)\/makefile)/si ; print $2; or $str=~/(.*)\s(.*\/makefile)/si ; print $2; outputs: **************** ..\../tools/cell/makefile ..\../system-build/ali/text/makefile

    Regards
    Senthil Kumar.k