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

hi monks

Can you help me with a regular expression to match the foll:
/sbc/dasds/sdf/dsd/modem
ie.., the first four occurences sbc/dasds/sdf/dsd/should match absolutely and the last occurence should be matched in such a way such that anything starting with modem should match

I hope i have made it clear
if any clarifications ..pls ask me

Thanks®ards
lax

Replies are listed 'Best First'.
Re: Regular expression
by Zaxo (Archbishop) on Jun 22, 2006 at 05:11 UTC

    Matching what you show as literal text should do the job. You probably want to anchor the beginnning to the start of the string. A literal match like this can be done with substr and eq, too.

    my $string = '/sbc/dasds/sdf/dsd/modem'; my $regex = qr!^${string}!; $_ = '/sbc/dasds/sdf/dsd/modem_50'; if (m/$regex/) { print; print "\nRegex works\n"; } if ($string eq substr $_, 0, length $string) { print; print "\nString equality works\n"; }
    If you're doing this to comb modems out of a bunch of file paths, there is also glob:
    my @modems = glob '/sbc/dasds/sdf/dsd/modem*';

    After Compline,
    Zaxo

Re: Regular expression
by GrandFather (Saint) on Jun 22, 2006 at 05:01 UTC

    What have you tried? Have you read the tutorial section for regular expressions?. Have you read the standard Perl documentation perlrequick, perlretut and perlre?

    You are more likely to get a direct answer if you ask in the chatterbox, but then again, maybe not. :) We like to see some effort made.


    DWIM is Perl's answer to Gödel
Re: Regular expression
by l.frankline (Hermit) on Jun 22, 2006 at 05:20 UTC

    hi,

    If my guess is correct, then follow the below said code:

    while (<DATA>) { print "$&\n" if ($_=~/^\/(.+)modem$/); } __DATA__ /1sbc/dasds/sdf/dsd/modem /2sbc/dasds/FSFsdf/dsd/modem1 /3sbc/dasdFDAs/sFDASFdf/dsd/modem /4sbc/dasFSDAds/sdf/dFDsd/modem3

    output

    /1sbc/dasds/sdf/dsd/modem
    /3sbc/dasdFDAs/sFDASFdf/dsd/modem

    regards

    franklin

    Don't put off till tomorrow, what you can do today.

      But the requirement was first to match sbc/dasds/sdf/dsd/ absolutely and after that anything starts with "modem".
      Your code matches the occurance of modem as the last string alone which I believe is not what is required.
      my $string ="sbc/dasds/sdf/dsd/modemL" ; if ($string=~/sbc\/dasds\/sdf\/dsd\/modem.*/) { print "Matching" ; }
      should be sufficient.
      Thanks..
Re: Regular expression
by rsriram (Hermit) on Jun 22, 2006 at 05:45 UTC

    Try this,

    if($_=~/sbc\/dasds\/sdf\/dsd\/modem(.*)/g)
        {
        ##your operation if search returns true
        }

    Sriram