Regular expression - first two alphabets of a string

R

Ravi Singh

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,
 
A

Abhijit Gadgil

ZGVmIG1hdGNoYWEgKHN0cikKCiAgaWYgc3RyLm1hdGNoKC9eYWEvKQogICAgcmV0dXJuIHRydWUK
ICBlbHNlCiAgICAgcmV0dXJuIGZhbHNlCiAgZW5kCmVuZAoKT24gNS8zLzA3LCBSYXZpIFNpbmdo
IDxib2JhZ2VudEBnbWFpbC5jb20+IHdyb3RlOgo+IEhpIEFsbCwKPgo+IEkgbGVhcm5pbmcgcmVn
dWxhciBleHByZXNzaW9ucyBpbiBSdWJ5IGFuZCBJIHdhbnQgdG8gZG8gdGhlIGZvbGxvd2luZwo+
IHRoaW5nIHdpdGggYSBzdHJpbmdzCj4KPiAiQSBzdGluZyBzaG91bGQgbm90IHN0YXJ0IHdpdGgg
dHdvIGEgaS5lIGFhIgo+Cj4gZXhhbXBsZQo+Cj4gImFhIHh5eiIgLSB3cm9uZwo+ICJheHl6IiAt
IGlzIGZpbmUKPiAiYSB4eXoiIGlzIGZpbmUKPgo+IHBsZWFzZSBoZWxwLgo+Cj4gUmVnYXJkcywK
Pgo+IC0tCj4gUG9zdGVkIHZpYSBodHRwOi8vd3d3LnJ1YnktZm9ydW0uY29tLy4KPgo+CgoKLS0g
CuCkheCkreCkv+CknOClgOCkpAoKWyB3cml0dGVuIGluIGh0dHA6Ly93d3cucGFhaGlqZW4uY29t
L3NjcmF0Y2hwYWQgXQoKWyBodHRwOi8vd3d3LnBhYWhpamVuLmNvbSBdCg==
 
R

Ravi Singh

Abhijit,

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

Regards,
 
R

Robert Klemme

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
 
R

Rick DeNatale

def matchaa (str)

if str.match(/^aa/)
return true
else
return false
end
end

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

Abhijit Gadgil

c29ycnkhIEkgbWlzcmVhZCB5b3VyIHBvc3QhIDotKAoKT24gNS8zLzA3LCBSYXZpIFNpbmdoIDxi
b2JhZ2VudEBnbWFpbC5jb20+IHdyb3RlOgo+IEFiaGlqaXQsCj4KPiBJIHRyaWVkIHRoZSBtZXRo
b2QgZm9yICJhYSB4eXoiIGFuZCBpdCByZXR1cm4gdHJ1ZSBhY3R1YWxseSBpdCBzaG91bGQKPiBy
ZXR1cm4gZmFsc2UuCj4KPiBSZWdhcmRzLAo+Cj4gLS0KPiBQb3N0ZWQgdmlhIGh0dHA6Ly93d3cu
cnVieS1mb3J1bS5jb20vLgo+Cj4KCgotLSAK4KSF4KSt4KS/4KSc4KWA4KSkCgpbIHdyaXR0ZW4g
aW4gaHR0cDovL3d3dy5wYWFoaWplbi5jb20vc2NyYXRjaHBhZCBdCgpbIGh0dHA6Ly93d3cucGFh
aGlqZW4uY29tIF0K
 
R

Robert Klemme

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
 
R

Rick DeNatale

On 03.05.2007 20:30, Rick DeNatale wrote:

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/
 

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

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top