Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Regular expression - first two alphabets of a string

Reply
Thread Tools

Regular expression - first two alphabets of a string

 
 
Ravi Singh
Guest
Posts: n/a
 
      05-03-2007
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/.

 
Reply With Quote
 
 
 
 
Abhijit Gadgil
Guest
Posts: n/a
 
      05-03-2007
ZGVmIG1hdGNoYWEgKHN0cikKCiAgaWYgc3RyLm1hdGNoKC9eYW EvKQogICAgcmV0dXJuIHRydWUK
ICBlbHNlCiAgICAgcmV0dXJuIGZhbHNlCiAgZW5kCmVuZAoKT2 4gNS8zLzA3LCBSYXZpIFNpbmdo
IDxib2JhZ2VudEBnbWFpbC5jb20+IHdyb3RlOgo+IEhpIEFsbC wKPgo+IEkgbGVhcm5pbmcgcmVn
dWxhciBleHByZXNzaW9ucyBpbiBSdWJ5IGFuZCBJIHdhbnQgdG 8gZG8gdGhlIGZvbGxvd2luZwo+
IHRoaW5nIHdpdGggYSBzdHJpbmdzCj4KPiAiQSBzdGluZyBzaG 91bGQgbm90IHN0YXJ0IHdpdGgg
dHdvIGEgaS5lIGFhIgo+Cj4gZXhhbXBsZQo+Cj4gImFhIHh5ei IgLSB3cm9uZwo+ICJheHl6IiAt
IGlzIGZpbmUKPiAiYSB4eXoiIGlzIGZpbmUKPgo+IHBsZWFzZS BoZWxwLgo+Cj4gUmVnYXJkcywK
Pgo+IC0tCj4gUG9zdGVkIHZpYSBodHRwOi8vd3d3LnJ1YnktZm 9ydW0uY29tLy4KPgo+CgoKLS0g
CuCkheCkreCkv+CknOClgOCkpAoKWyB3cml0dGVuIGluIGh0dH A6Ly93d3cucGFhaGlqZW4uY29t
L3NjcmF0Y2hwYWQgXQoKWyBodHRwOi8vd3d3LnBhYWhpamVuLm NvbSBdCg==

 
Reply With Quote
 
 
 
 
Ravi Singh
Guest
Posts: n/a
 
      05-03-2007
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/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      05-03-2007
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
 
Reply With Quote
 
Rick DeNatale
Guest
Posts: n/a
 
      05-03-2007
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/

 
Reply With Quote
 
Ravi Singh
Guest
Posts: n/a
 
      05-03-2007
Thanks Rick -ve lookahead helped.

Regards,

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

 
Reply With Quote
 
Abhijit Gadgil
Guest
Posts: n/a
 
      05-04-2007
c29ycnkhIEkgbWlzcmVhZCB5b3VyIHBvc3QhIDotKAoKT24gNS 8zLzA3LCBSYXZpIFNpbmdoIDxi
b2JhZ2VudEBnbWFpbC5jb20+IHdyb3RlOgo+IEFiaGlqaXQsCj 4KPiBJIHRyaWVkIHRoZSBtZXRo
b2QgZm9yICJhYSB4eXoiIGFuZCBpdCByZXR1cm4gdHJ1ZSBhY3 R1YWxseSBpdCBzaG91bGQKPiBy
ZXR1cm4gZmFsc2UuCj4KPiBSZWdhcmRzLAo+Cj4gLS0KPiBQb3 N0ZWQgdmlhIGh0dHA6Ly93d3cu
cnVieS1mb3J1bS5jb20vLgo+Cj4KCgotLSAK4KSF4KSt4KS/4KSc4KWA4KSkCgpbIHdyaXR0ZW4g
aW4gaHR0cDovL3d3dy5wYWFoaWplbi5jb20vc2NyYXRjaHBhZC BdCgpbIGh0dHA6Ly93d3cucGFh
aGlqZW4uY29tIF0K

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      05-04-2007
On 03.05.2007 20:30, Rick DeNatale wrote:
> 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>


What exactly do you gain by using lookahead instead of a normal match?

Kind regards

robert
 
Reply With Quote
 
Rick DeNatale
Guest
Posts: n/a
 
      05-04-2007
On 5/4/07, Robert Klemme <> 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.denhaven2.com/

IPMS/USA Region 12 Coordinator
http://ipmsr12.denhaven2.com/

Visit the Project Mercury Wiki Site
http://www.mercuryspacecraft.com/

 
Reply With Quote
 
Ravi Singh
Guest
Posts: n/a
 
      05-04-2007
Robert Klemme wrote:
> On 03.05.2007 20:30, Rick DeNatale wrote:
>>> On 5/3/07, Ravi Singh <> 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/lookaround.html

Regards,

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
test a string has only alphabets [a-zA-Z] Matt Java 4 08-21-2004 03:48 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57