in reply to Re: doubt in string matching.
in thread doubt in string matching.

I am affraid your first solution does not seem to work. Pls correct me if I am wrong.
Say ,
my $find="CXYF"; my $string = "ABCDEFGHIJKLMNOPCDEFQRST";
In this case also, the output will be
'CDEF' string found
Just to correct
#!/usr/bin/perl -w use strict; my $string= "ABCDEFGHIJKLMNOPCDEFQRST"; my $find="CDEF"; #extract first and last character my ($first, $last) = $find =~ /([A-Z])[A-Z]*([A-Z])/; #check both character present in string if ($string =~ /$first/ && $string =~ /$last/) { print "'$find' string found" if ($string =~ /$find/); }
This combines your first and second solution. Thanks..

Replies are listed 'Best First'.
Re^3: doubt in string matching.
by prasadbabu (Prior) on Oct 17, 2006 at 05:50 UTC

    Mandrake You are right, if the $find string is "CXYF". But my solution is based on the OP's logic. If the strings are "CDEF", "QRST" etc. it ll work perfectly. The OP has given,

    i keep incrementing first(after finding C)

    Prasad

      Sorry but unless you perform a check at each increment, this incrementing does not prove good. Your implementation just checks the first and last characters only. This will return success if
      $find = "CDED"; # or $find = "CDEH"; # or $find = "CDEJ"; #or even $find = "CDEC";
      Also if you check the pattern closely,
      BCDEFGHIJKLMNOPCDEFQRST
      you can find a CDEF in-between the P and Q. So incrementing the string from $first..$last should not be the approach here.
      I believe by "incrementing" what OP meant was incrementing the search string from first to last position. Thanks..

        Mandrake You are absolutely correct, but OP has not given enough samples of input and he has not given the exact requirement clearly, which I mentioned in my first reply itself and the samething was highlighted by ikegami and chargrill. So I assumed he might have inputs with incremented characters inbetween instead of random characters.

        ikegami has mentioned

        We can't help you if you can't even describe what you want accuratly

        chargrill has mentioned

        I don't know what you mean. Solid examples will speak volumes, show us what output you want to get.

        Prasad