Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

how to search for a pattern in a string within a certain range

by BhariD (Sexton)
on Mar 08, 2010 at 03:30 UTC ( [id://827307]=perlquestion: print w/replies, xml ) Need Help??

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

If I have to search for a pattern in a string within certain range of the string then what should I specify?

For example

I have the following string:

string1: ATGGCTAGTTTCAGT string2: GTTTATGGGTACGTGT

the search pattern is 'TTT'. I want to search the pattern in the start of the string (not in the middle or in the end), or only up till 8 characters, once pattern is identified, remove the characters before the pattern along with the pattern. so my output will be like:

string1: ATGGCTAGTTTCAGT string2: ATGGGTACGTGT

Can anyone help me please?

Thanks

Replies are listed 'Best First'.
Re: how to search for a pattern in a string within a certain range
by ikegami (Patriarch) on Mar 08, 2010 at 03:37 UTC

    This is so similar to your last question. So you want to match at most 8 characters before the sequence instead of any number of characters. Did you really not see anything relevant in perlre? What did you try?

Re: how to search for a pattern in a string within a certain range
by johngg (Canon) on Mar 08, 2010 at 13:41 UTC

    Rather than using a regular expression, you might want to consider using index to find the first occurance of your search pattern. If it is found within your initial range then you could follow Ratazong's advice and use substr to remove characters as required.

    I hope this is helpful.

    Cheers,

    JohnGG

      My toughts, too. Regex's seem like an overly complicated solution to me; but, on the other hand, it's a good problem to learn some pretty empowering Regex strategies. So "Your mileage may vary."

      Here is a quick sript that uses substr() and index() to find the search-for pattern and deletes everything up to the pattern plus that pattern. It also only searches up to the specified number of characters.

      This script produces the following output:

      ATGGCTAGTTTCAGT ---> ATGGCTAGTTTCAGT GTTTATGGGTACGTGT ---> ATGGGTACGTGT ATTGATACGGGTATTTAGGCTG ---> ATTGATACGGGTATTTAGGCTG ATTAGGAAGTAGGACCCTAGG ---> ATTAGGAAGTAGGACCCTAGG

      If I understand the OP's inquiry, I think this does what is wanted.

      The only thing I'm not clear on is what the OP expects to do if there are (1) multiple occurances, within the seek-or number of characters, of the search-for pattern in the string? and (2) what the result should be if there is a run of the search-for pattern (e.g., 'TTTTTTTTT' which has three intances of the search-for pattern in a run)?

      ack Albuquerque, NM
Re: how to search for a pattern in a string within a certain range
by Ratazong (Monsignor) on Mar 08, 2010 at 07:24 UTC

    Apart from Ikegami's suggestion, you might also reduce your string to its first 8 or 11 characters (*) and then apply your pattern-matching on the reduced string. Use substr for reducing the string.

    * depending whether the TTT needs to be contained in the 8 characters or not

      you might also reduce your string to its first 8 or 11 characters (*)

      8 or 10 please.

      If the substring has to start within the first 8 characters, it can only extend 2 two character over the first 8. A typical Off-by-one error :-)

      Perl 6 - links to (nearly) everything that is Perl 6.
Re: how to search for a pattern in a string within a certain range
by Sinistral (Monsignor) on Mar 08, 2010 at 14:59 UTC

    Considering the strings you are using contain A,T,G,C, I'm guessing you're trying to some sort of genetic analysis. I highly recommend you head on over to the BioPerl site and start reading. They have optimized solutions in Perl for handling huge data sets of nucleotide sequences and doing matching and reporting.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-23 22:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found