himanshu.padmanabhi has asked for the wisdom of the Perl Monks concerning the following question:

This is entry from 'fstab'.
$line="/dev/mapper/VGNEW1-Lv2 /volumes/VGNEW1/lvabc ext3 rw,grpquota,u +srquota,acl 0 0"; if($line =~ m/\/volumes\/VGNEW1\/lv.../) { ...some code }
So 'lv...' matches with 'lvabc' which I dont want.Another way is to split 'fstab' line and directly check 2nd argument.But If how can I do it with regular expression? 'index' function also doesn't solve the purpose.

Replies are listed 'Best First'.
Re: Matching exact string
by CountZero (Bishop) on Mar 24, 2009 at 07:06 UTC
    Does m/\/volumes\/VGNEW1\/lv\b/ work? It will now match only if the "word" ends after "/lv".

    Splitting can be done as follows:

    use strict; use warnings; my $line = "/dev/mapper/VGNEW1-Lv2 /volumes/VGNEW1/lvabc ext3 rw,grpquota,u +srquota,acl 0 0"; my @items = split / /, $line; my @mount_options = split /,/, $items[3];

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Matching exact string
by rovf (Priest) on Mar 24, 2009 at 09:07 UTC
    'lv...' matches with 'lvabc' which I dont want.

    What, then, do you want to match? Maybe you should explain first what the code is supposed to do...

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: Matching exact string
by targetsmart (Curate) on Mar 24, 2009 at 12:28 UTC
    try this
    if($line =~ m#/volumes/VGNEW1/lv[^a][^b][^c]#) {
    you have said lv... and told that you don't want to match lvabc, then try the above, but i think it will match anything other than a b and c at the appropriate positions, give more clarity on your possible kinds of inputs and expected match.
    read perlretut

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
      Thank you all really.Problem is solved.
Re: Matching exact string
by apl (Monsignor) on Mar 24, 2009 at 11:53 UTC
    If you wish to match lv... you need to specify /lv\.\.\./. What you did was to say "I wish to match lower-case L, lower-case V and any three characters". "lvabc" certainly fits that description.
Re: Matching exact string
by linuxer (Curate) on Mar 24, 2009 at 17:52 UTC

    Although you already mentioned, that your problem is solved, I think this is another solution:

    if ( $line =~ m{/volumes/VGNEW1/lv(?!abc)} ) { # your code }

    It should match any /volumes/VGNEW1/lv which is not followed by abc. See perlre ('zero-width negative look-ahead assertion') for details.

Re: Matching exact string
by Anonymous Monk on Mar 24, 2009 at 07:03 UTC
    why did you put ...?
      "lv..." can be one of the input strings.
Re: Matching exact string
by VinsWorldcom (Prior) on Mar 24, 2009 at 12:00 UTC

    If you only want 'lv', use m/\/volumes\/VGNEW1\/(lv)\.\.\./

    $1 will now be "lv".