Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: doubt in string matching.

by prasadbabu (Prior)
on Oct 17, 2006 at 05:02 UTC ( [id://578662]=note: print w/replies, xml ) Need Help??


in reply to doubt in string matching.

heidi,

ikegami++

You have not clearly given what you are going to achieve. The following works as per your logic.

use strict; use warnings; my ($first, $last) = $find =~ /([A-Z])[A-Z]*([A-Z])/; #extract first a +nd last character if ($string =~ /$first/ && $string =~ /$last/) #check both character p +resent in string { my $out; for ($first..$last) #increment inbetween characters { $out = $out.$_ ; } print "'$find' string found" if ($string =~ /$out/); # if found pr +int the string }
or Simply
my $string = "ABCDEFGHIJKLMNOPCDEFQRST"; my $find="CDEF"; print "'$find' string found" if ($string =~ /$find/);

Prasad

Replies are listed 'Best First'.
Re^2: doubt in string matching.
by Mandrake (Chaplain) on Oct 17, 2006 at 05:39 UTC
    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..

      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..

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://578662]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-26 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found