Regex query

M

Manoj P M

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
 
L

Luis Parravicini

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top