# Regex query

**URL:** <https://www.ruby-forum.com/t/regex-query/81592>\
**Category:** Ruby\
**Created:** [January 10, 2007, 11:33am UTC](https://www.ruby-forum.com/t/regex-query/81592 "2007-01-10T11:33:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manoj\_P\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@Manoj\_P\_M](https://www.ruby-forum.com/u/Manoj_P_M)\
**Post date:** [January 10, 2007, 11:33am UTC](https://www.ruby-forum.com/t/regex-query/81592/1 "2007-01-10T11:33:15Z")

</div>

Hi,

A Simple question on regular expression in ruby. Some can make me  
understand the below code snippets. I was under the impression that  
both these statement would give the same answer. But it doesnt. Why ?

irb(main):001:0\> ‘banana’ =~ /(an)\*/  
=\> 0  
irb(main):002:0\> ‘banana’ =~ /(an)+/  
=\> 1

-Manjo

---

<div class="post-metadata">

**Author:** ![Manoj\_P\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@Manoj\_P\_M](https://www.ruby-forum.com/u/Manoj_P_M)\
**Post date:** [January 10, 2007, 12:03pm UTC](https://www.ruby-forum.com/t/regex-query/81592/2 "2007-01-10T12:03:21Z")

</div>

On 1/10/07, Manoj P M [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> A Simple question on regular expression in ruby. Some can make me  
> understand the below code snippets. I was under the impression that  
> both these statement would give the same answer. But it doesnt. Why ?
> 
> irb(main):001:0\> ‘banana’ =~ /(an)\*/  
> =\> 0

In this case you are looking for ‘’, ‘an’, ‘anan’, ‘ananan’ and so on.  
So it matches at the beginning of the string (index 0) with the empty  
string.

Luis

---

<div class="post-metadata">

**Author:** ![Manoj\_P\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@Manoj\_P\_M](https://www.ruby-forum.com/u/Manoj_P_M)\
**Post date:** [January 10, 2007, 12:07pm UTC](https://www.ruby-forum.com/t/regex-query/81592/3 "2007-01-10T12:07:26Z")

</div>

On 1/10/07, Manoj P M [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> A Simple question on regular expression in ruby. Some can make me  
> understand the below code snippets. I was under the impression that  
> both these statement would give the same answer. But it doesnt. Why ?
> 
> irb(main):001:0\> ‘banana’ =~ /(an)\*/  
> =\> 0

this is happening because \* means 0 or more times match the preceding  
literal. here always the regex match =~ returns the position in the  
string  
where it finds the match and starts from 0 (as in C and ruby the array  
parameter starts count from 0 like if array=‘sachin’ and then array[0]  
will  
give ‘s’)  
so in the first case it matches ‘b’ and returns zero since it tries to  
match  
‘an’ matched 0 times so returns the position taken by ‘b’

irb(main):002:0\> ‘banana’ =~ /(an)+/

> =\> 1

in this case the place where it matches ‘an’ is starting at b(0) a(1)  
n(2)  
a(3) n(4) a(5) so returns 1 since ‘an’ is matched beginning at 1  
location.

I hope this clarifies

-Manjo

> 

–  
removed\_email\_address@domain.invalid

URL : [http://nichas143.tripod.com](http://nichas143.tripod.com)
