# Search pattern

**URL:** <https://www.ruby-forum.com/t/search-pattern/125644>\
**Category:** Ruby\
**Created:** [January 10, 2008, 10:29pm UTC](https://www.ruby-forum.com/t/search-pattern/125644 "2008-01-10T22:29:31Z")\
**Posts on this page:** 8\
**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, 10:29pm UTC](https://www.ruby-forum.com/t/search-pattern/125644/1 "2008-01-10T22:29:31Z")

</div>

Hi all,

I have a Ruby script as follow. What I try to do is to find a pattern in  
the string and report its position. I wonder how to get the position of  
pattern in the string.

Thank you in advance,

Li

seq=“5’aaaAAAAAACCCCCCCCTTTTTTTGGGGGGGGG-3’”

seq.upcase!# change to upper case  
puts “old sequence:”  
puts seq.gsub!(/[^A-Z]/,’’)# extract the string  
puts pattern if seq=~/AC/

#how to get the position of pattern in the string?

---

<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:39pm UTC](https://www.ruby-forum.com/t/search-pattern/125644/2 "2008-01-10T22:39:37Z")

</div>

Alle giovedÃ¬ 10 gennaio 2008, Li Chen ha scritto:

> seq=“5’aaaAAAAAACCCCCCCCTTTTTTTGGGGGGGGG-3’”
> 
> seq.upcase!# change to upper case  
> puts “old sequence:”  
> puts seq.gsub!(/[^A-Z]/,’’)# extract the string  
> puts pattern if seq=~/AC/
> 
> #how to get the position of pattern in the string?

String=~ returns the position of the starting of the match, so

seq=~pattern

will return what you want.

Stefano

---

<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, 11:52pm UTC](https://www.ruby-forum.com/t/search-pattern/125644/3 "2008-01-10T23:52:55Z")

</div>

Stefano C. wrote:

> Alle giovedÃ¬ 10 gennaio 2008, Li Chen ha scritto:
> 
> > seq=“5’aaaAAAAAACCCCCCCCTTTTTTTGGGGGGGGG-3’”
> > 
> > seq.upcase!# change to upper case  
> > puts “old sequence:”  
> > puts seq.gsub!(/[^A-Z]/,’’)# extract the string  
> > puts pattern if seq=~/AC/
> > 
> > #how to get the position of pattern in the string?
> 
> String=~ returns the position of the starting of the match, so
> 
> seq=~pattern
> 
> will return what you want.
> 
> Stefano  
> The size if seq is 33. What I try to do is starting position of “AC” in  
> the seq.

How do I calculate the position of it?

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 11, 2008, 1:29pm UTC](https://www.ruby-forum.com/t/search-pattern/125644/4 "2008-01-11T13:29:40Z")

</div>

Li Chen wrote:

> How do I calculate the position of it?  
> For some reason I have the Jeopardy melody in my head now…  
> Anyway:

> > seq=“AACGTGTAGGCTTGAAA”  
> > =\> “AACGTGTAGGCTTGAAA”
> 
> > pattern = /TA/  
> > =\> TA
> 
> > position = seq =~ pattern  
> > =\> 6
> 
> > seq[position,2]  
> > =\> “TA”
> 
> > seq[position…-1]  
> > =\> “TAGGCTTGAAA”
> 
> > seq[pattern]  
> > =\> “TA”

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 11, 2008, 12:34am UTC](https://www.ruby-forum.com/t/search-pattern/125644/5 "2008-01-11T00:34:37Z")

</div>

Li Chen wrote:

> > seq=~pattern
> > 
> > will return what you want.
> > 
> > Stefano  
> > The size if seq is 33. What I try to do is starting position of “AC” in  
> > the seq.
> 
> How do I calculate the position of it?
> 
> Thanks,
> 
> Li  
> You’re so close.

seq=“5’aaaAAAAAACCCCCCCCTTTTTTTGGGGGGGGG-3’”

seq.upcase!# change to upper case  
puts “old sequence:”  
puts seq.gsub!(/[^A-Z]/,’’)# extract the string  
puts position = seq=~/AC/  
#The counting starts at zero. Or use the more simple but less powerfull  
puts position = seq.index(“AC”)

regards,

Siep

---

<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 11, 2008, 7:34pm UTC](https://www.ruby-forum.com/t/search-pattern/125644/6 "2008-01-11T19:34:12Z")

</div>

Li Chen wrote:

> Now I have a question: is it possible in Ruby to highlight or underscore  
> or use different font size for the found pattern?

That depends on how/where you output the data, obviously. If you’re  
writing a  
GUI app or a webpage you can, of course, go as crazy with the font as  
you  
want to.  
In terminal apps you don’t have control about font size or face.  
Changing  
color, inverting color or underlining is possible, but terminal  
dependant  
(specifically I don’t think the terminal in Windows handles ANSI escape  
sequences). There probably are gems to abstract away the differences  
between  
platforms, though.

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 11, 2008, 7:24pm UTC](https://www.ruby-forum.com/t/search-pattern/125644/7 "2008-01-11T19:24:19Z")

</div>

Hi all,

Thank you all for the help.

Now I have a question: is it possible in Ruby to highlight or underscore  
or use different font size for the found pattern?

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 11, 2008, 8:59pm UTC](https://www.ruby-forum.com/t/search-pattern/125644/8 "2008-01-11T20:59:08Z")

</div>

Sebastian H. wrote:

> Li Chen wrote:
> 
> > Now I have a question: is it possible in Ruby to highlight or underscore  
> > or use different font size for the found pattern?
> 
> That depends on how/where you output the data, obviously. If you’re  
> writing a  
> GUI app or a webpage you can, of course, go as crazy with the font as  
> you  
> want to.  
> In terminal apps you don’t have control about font size or face.  
> Changing  
> color, inverting color or underlining is possible, but terminal  
> dependant  
> (specifically I don’t think the terminal in Windows handles ANSI escape  
> sequences). There probably are gems to abstract away the differences  
> between  
> platforms, though.
> 
> HTH,  
> Sebastian

Thank you so much,

Li
