# String extraction

**URL:** <https://www.ruby-forum.com/t/string-extraction/125639>\
**Category:** Ruby\
**Created:** [January 10, 2008, 9:38pm UTC](https://www.ruby-forum.com/t/string-extraction/125639 "2008-01-10T21:38:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 10, 2008, 9:38pm UTC](https://www.ruby-forum.com/t/string-extraction/125639/1 "2008-01-10T21:38:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 10, 2008, 9:47pm UTC](https://www.ruby-forum.com/t/string-extraction/125639/2 "2008-01-10T21:47:13Z")

</div>

On Jan 10, 2:38 pm, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Posted viahttp://www.ruby-forum.com/.

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]/, ‘’)

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 10, 2008, 9:49pm UTC](https://www.ruby-forum.com/t/string-extraction/125639/3 "2008-01-10T21:49:36Z")

</div>

Hi –

On 10/01/2008, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 A.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 10, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/string-extraction/125639/4 "2008-01-10T21:50:10Z")

</div>

yermej wrote:

> On Jan 10, 2:38?pm, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > –  
> > Posted viahttp://www.ruby-forum.com/.
> 
> 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

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 10, 2008, 9:52pm UTC](https://www.ruby-forum.com/t/string-extraction/125639/5 "2008-01-10T21:52:35Z")

</div>

Li Chen wrote:

> What is the Ruby way to extract a string like this?
> 
> old\_string=“5’-AAAAAAAA Â Â CCCCCCCC Â TTT Â GGGGG Â Â GGGG-3’”  
> new\_string=“AAAAAAAACCCCCCCCTTTGGGGGGGGG”

new\_string = old\_string.gsub(/[^ACTG]/i,"")

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

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 10, 2008, 10:03pm UTC](https://www.ruby-forum.com/t/string-extraction/125639/6 "2008-01-10T22:03:03Z")

</div>

yermej wrote:

> On Jan 10, 2:38?pm, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > –  
> > Posted viahttp://www.ruby-forum.com/.
> 
> 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

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 10, 2008, 10:22pm UTC](https://www.ruby-forum.com/t/string-extraction/125639/7 "2008-01-10T22:22:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 10, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/string-extraction/125639/8 "2008-01-10T22:30:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 10, 2008, 10:09pm UTC](https://www.ruby-forum.com/t/string-extraction/125639/9 "2008-01-10T22:09:03Z")

</div>

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”)
