![]() |
Any regular expressions gurus here? - Printable Version +- MacResource (https://forums.macresource.com) +-- Forum: My Category (https://forums.macresource.com/forumdisplay.php?fid=1) +--- Forum: Tips and Deals (https://forums.macresource.com/forumdisplay.php?fid=3) +--- Thread: Any regular expressions gurus here? (/showthread.php?tid=69260) |
Any regular expressions gurus here? - volcs0 - 12-29-2008 I want to take a string (a protein sequence) and replace all the instances of "K*" and "R*" (where * is anything) with "K,*" and "R,*" respectively. In other words, every time a K or an R appears, I want to place a comma after it. The exception is any time there is a P following the K or R - then no insert. This is how the enzyme trypsin acts. I'm trying to do this in python, but I'm not very good at regular expressions. I could iterate through the sequences, but I think regex will be much faster. For instance: MVLTIYPDELVQIVSDKIASNRGKITLNQLWDISGKPFDLSDKKVKQFVLSCVILKKDI MVLTIYPDELVQIVSDK,IASNR,GK,ITLNQLWDISGKPFDLSDK,K,VK,QFVLSCVILK,K,DI Any help is appreciated. I will keep reading and post back if I figure it out. Thanks. Re: Any regular expressions gurus here? - TheTominator - 12-29-2008 How about this one? I'm not sure how Python does regexps. This should be suitable for Perl-alikes. s/([KR])[^P]/\1,/ If I got the memory part of the replacement expression wrong, you can do it in two passes with s/K[^P]/K,/ s/R[^P]/R,/ Re: Any regular expressions gurus here? - TheCaber - 12-29-2008 TheTominator wrote: s/([KR])([^P])/\1,\2/ will not strip off the non-P following a K-or-R match Re: Any regular expressions gurus here? - TheTominator - 12-29-2008 TheCaber wrote: s/([KR])([^P])/\1,\2/ will not strip off the non-P following a K-or-R match Oh yeah. Good catch. I always do programming in two stages. Stage 1: Bugging Stage 2: Debugging Re: Any regular expressions gurus here? - volcs0 - 12-30-2008 Thanks. This is the python code that did it. I could not do it in one pass. There is no global flag for python for regex. def trypsinize(proteins): |