Robert Klemme
5/4/2007 7:34:00 AM
On 03.05.2007 20:30, Rick DeNatale wrote:
> On 5/3/07, Abhijit Gadgil <gabhijit@gmail.com> wrote:
>> def matchaa (str)
>>
>> if str.match(/^aa/)
>> return true
>> else
>> return false
>> end
>> end
>>
>> On 5/3/07, Ravi Singh <bobagent@gmail.com> 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>
What exactly do you gain by using lookahead instead of a normal match?
Kind regards
robert