[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regular expression - first two alphabets of a string

Ravi Singh

5/3/2007 5:15:00 PM

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

please help.

Regards,

--
Posted via http://www.ruby-....

9 Answers

Abhijit Gadgil

5/3/2007 5:34:00 PM

0

def matchaa (str) if str.match(/^aa/) return true else return false endendOn 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>> please help.>> Regards,>> --> Posted via http://www.ruby-forum.com/.... ??????[ written in http://www.pa.../... ][ http://www.pa... ]

Ravi Singh

5/3/2007 6:25:00 PM

0

Abhijit,

I tried the method for "aa xyz" and it return true actually it should
return false.

Regards,

--
Posted via http://www.ruby-....

Robert Klemme

5/3/2007 6:29:00 PM

0

On 03.05.2007 19:33, Abhijit Gadgil wrote:
> def matchaa (str)
>
> if str.match(/^aa/)
> return true
> else
> return false
> end
> end

This does not seem to be worth a method...

raise "Illegal String: #{str}" if /\Aaa/ =~ str

Kind regards

robert

Rick DeNatale

5/3/2007 6:31:00 PM

0

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>


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Ravi Singh

5/3/2007 7:47:00 PM

0

Thanks Rick -ve lookahead helped.

Regards,

--
Posted via http://www.ruby-....

Abhijit Gadgil

5/4/2007 4:11:00 AM

0

sorry! I misread your post! :-(On 5/3/07, Ravi Singh <bobagent@gmail.com> wrote:> Abhijit,>> I tried the method for "aa xyz" and it return true actually it should> return false.>> Regards,>> --> Posted via http://www.ruby-forum.com/.... ??????[ written in http://www.pa.../... ][ http://www.pa... ]

Robert Klemme

5/4/2007 7:34:00 AM

0

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

Rick DeNatale

5/4/2007 6:50:00 PM

0

On 5/4/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> On 03.05.2007 20:30, Rick DeNatale wrote:

> >
> > 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?

The OP was asking for an RE which matched anything which DIDN'T start with aa.

The proposal using if else got it backwards.

Of course you could also just negate the results of the match and use

!str.match(/aa/)

If you just wanted a test.



--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

IPMS/USA Region 12 Coordinator
http://ipmsr12.denh...

Visit the Project Mercury Wiki Site
http://www.mercuryspace...

Ravi Singh

5/4/2007 11:46:00 PM

0

Robert Klemme wrote:
> On 03.05.2007 20:30, Rick DeNatale wrote:
>>> On 5/3/07, Ravi Singh <bobagent@gmail.com> wrote:
>>> > "axyz" - is fine
>> 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

When Rick directed me to use -ve lookahead I reach the following page
and it made the point clear.

http://www.regular-expressions.info/looka...

Regards,

--
Posted via http://www.ruby-....