[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

string extraction

Li Chen

1/10/2008 8:39:00 PM

Hi all,

What is the Ruby way to extract a string like this?

old_string="5'-AAAAAAAA CCCCCCCC TTT GGGGG GGGG-3'"
new_string="AAAAAAAACCCCCCCCTTTGGGGGGGGG"


Thank you very much,

Li
--
Posted via http://www.ruby-....

8 Answers

yermej

1/10/2008 8:45:00 PM

0

On Jan 10, 2:38 pm, Li Chen <chen_...@yahoo.com> wrote:
> Hi all,
>
> What is the Ruby way to extract a string like this?
>
> old_string="5'-AAAAAAAA    CCCCCCCC  TTT   GGGGG    GGGG-3'"
> new_string="AAAAAAAACCCCCCCCTTTGGGGGGGGG"
>
> Thank you very much,
>
> Li
> --
> Posted viahttp://www.ruby-....

new_string = old_string.gsub(/[^A-Z]/, '')

That substitutes '' (empty string) for any character that is not (^) a
capital letter (A-Z).

To do the same thing in place: old_string.gsub!(/^A-Z]/, '')

Thomas Adam

1/10/2008 8:49:00 PM

0

Hi --

On 10/01/2008, Li Chen <chen_li3@yahoo.com> wrote:
> Hi all,
>
> What is the Ruby way to extract a string like this?
>
> old_string="5'-AAAAAAAA CCCCCCCC TTT GGGGG GGGG-3'"
> new_string="AAAAAAAACCCCCCCCTTTGGGGGGGGG"
>

old_string="5'-AAAAAAAA CCCCCCCC TTT GGGGG GGGG-3'".scan(/[A-Z]+/).to_s

And variations thereof.

-- Thomas Adam

Li Chen

1/10/2008 8:50:00 PM

0

yermej wrote:
> On Jan 10, 2:38?pm, Li Chen <chen_...@yahoo.com> wrote:
>> --
>> Posted viahttp://www.ruby-....
>
> new_string = old_string.gsub(/[^A-Z]/, '')
>
> That substitutes '' (empty string) for any character that is not (^) a
> capital letter (A-Z).
>
> To do the same thing in place: old_string.gsub!(/^A-Z]/, '')

Thank you so much.

Li
--
Posted via http://www.ruby-....

Sebastian Hungerecker

1/10/2008 8:52:00 PM

0

Li Chen wrote:
> What is the Ruby way to extract a string like this?
>
> old_string=3D"5'-AAAAAAAA =C2=A0 =C2=A0CCCCCCCC =C2=A0TTT =C2=A0 GGGGG =
=C2=A0 =C2=A0GGGG-3'"
> new_string=3D"AAAAAAAACCCCCCCCTTTGGGGGGGGG"

new_string =3D old_string.gsub(/[^ACTG]/i,"")

This removes anything that is not an A, C, T or G (case insensitively. If y=
ou=20
want lower case chars acgt to be removed, too, remove the i from the regex).


HTH,
Sebastian
=2D-=20
Jabber: sepp2k@jabber.org
ICQ: 205544826

Li Chen

1/10/2008 9:03:00 PM

0

yermej wrote:
> On Jan 10, 2:38?pm, Li Chen <chen_...@yahoo.com> wrote:
>> --
>> Posted viahttp://www.ruby-....
>
> new_string = old_string.gsub(/[^A-Z]/, '')
>
> That substitutes '' (empty string) for any character that is not (^) a
> capital letter (A-Z).
>
> To do the same thing in place: old_string.gsub!(/^A-Z]/, '')

Hi yermej,

One more question: How do I change all the As into T and all the Ts into
A in the old sttring?

old_s="AAATTAA"
new_s="TTTAATT"

thanks,

Li

--
Posted via http://www.ruby-....

Sebastian Hungerecker

1/10/2008 9:08:00 PM

0

Li Chen wrote:
> How do I change all the As into T and all the Ts into
> A in the old sttring?
>
> old_s="AAATTAA"
> new_s="TTTAATT"

new_s = old_s.tr("AT", "TA")
or if you also want GC
new_s = old_s.tr("ATGC", "TACG")

--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Li Chen

1/10/2008 9:23:00 PM

0

Sebastian Hungerecker wrote:
> Li Chen wrote:
>> How do I change all the As into T and all the Ts into
>> A in the old sttring?
>>
>> old_s="AAATTAA"
>> new_s="TTTAATT"
>
> new_s = old_s.tr("AT", "TA")
> or if you also want GC
> new_s = old_s.tr("ATGC", "TACG")

Thank you very much,

Li
--
Posted via http://www.ruby-....

James Gray

1/10/2008 9:29:00 PM

0

On Jan 10, 2008, at 2:38 PM, Li Chen wrote:

> Hi all,
>
> What is the Ruby way to extract a string like this?
>
> old_string="5'-AAAAAAAA CCCCCCCC TTT GGGGG GGGG-3'"

"5'-AAAAAAAA CCCCCCCC TTT GGGGG GGGG-3'".delete("^ACTG")

James Edward Gray II