On 5/3/07, Abhijit Gadgil <> wrote:
> def matchaa (str)
>
> if str.match(/^aa/)
> return true
> else
> return false
> end
> end
>
> On 5/3/07, Ravi Singh <> wrote:
> > Hi All,
> >
> > I learning regular expressions in Ruby and I want to do the following
> > thing with a strings
> >
> > "A sting should not start with two a i.e aa"
> >
> > example
> >
> > "aa xyz" - wrong
> > "axyz" - is fine
> > "a xyz" is fine
> >
Or use a negative lookahead:
irb(main):001:0> re = /^(?!aa)/
=> /^(?!aa)/
irb(main):002:0> "aa xyz".match(re)
=> nil
irb(main):003:0> "axyz".match(re)
=> #<MatchData:0xb7baf3e4>
irb(main):004:0> "a yz".match(re)
=> #<MatchData:0xb7bacb1c>
--
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/