Regular expression - first two alphabets of a string

Discussion in 'Ruby' started by Ravi Singh, May 3, 2007.

  1. Ravi Singh

    Ravi Singh Guest

    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/.
    Ravi Singh, May 3, 2007
    #1
    1. Advertising

  2. ZGVmIG1hdGNoYWEgKHN0cikKCiAgaWYgc3RyLm1hdGNoKC9eYWEvKQogICAgcmV0dXJuIHRydWUK
    ICBlbHNlCiAgICAgcmV0dXJuIGZhbHNlCiAgZW5kCmVuZAoKT24gNS8zLzA3LCBSYXZpIFNpbmdo
    IDxib2JhZ2VudEBnbWFpbC5jb20+IHdyb3RlOgo+IEhpIEFsbCwKPgo+IEkgbGVhcm5pbmcgcmVn
    dWxhciBleHByZXNzaW9ucyBpbiBSdWJ5IGFuZCBJIHdhbnQgdG8gZG8gdGhlIGZvbGxvd2luZwo+
    IHRoaW5nIHdpdGggYSBzdHJpbmdzCj4KPiAiQSBzdGluZyBzaG91bGQgbm90IHN0YXJ0IHdpdGgg
    dHdvIGEgaS5lIGFhIgo+Cj4gZXhhbXBsZQo+Cj4gImFhIHh5eiIgLSB3cm9uZwo+ICJheHl6IiAt
    IGlzIGZpbmUKPiAiYSB4eXoiIGlzIGZpbmUKPgo+IHBsZWFzZSBoZWxwLgo+Cj4gUmVnYXJkcywK
    Pgo+IC0tCj4gUG9zdGVkIHZpYSBodHRwOi8vd3d3LnJ1YnktZm9ydW0uY29tLy4KPgo+CgoKLS0g
    CuCkheCkreCkv+CknOClgOCkpAoKWyB3cml0dGVuIGluIGh0dHA6Ly93d3cucGFhaGlqZW4uY29t
    L3NjcmF0Y2hwYWQgXQoKWyBodHRwOi8vd3d3LnBhYWhpamVuLmNvbSBdCg==
    Abhijit Gadgil, May 3, 2007
    #2
    1. Advertising

  3. Ravi Singh

    Ravi Singh Guest

    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/.
    Ravi Singh, May 3, 2007
    #3
  4. 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
    Robert Klemme, May 3, 2007
    #4
  5. 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/
    Rick DeNatale, May 3, 2007
    #5
  6. Ravi Singh

    Ravi Singh Guest

    Ravi Singh, May 3, 2007
    #6
  7. c29ycnkhIEkgbWlzcmVhZCB5b3VyIHBvc3QhIDotKAoKT24gNS8zLzA3LCBSYXZpIFNpbmdoIDxi
    b2JhZ2VudEBnbWFpbC5jb20+IHdyb3RlOgo+IEFiaGlqaXQsCj4KPiBJIHRyaWVkIHRoZSBtZXRo
    b2QgZm9yICJhYSB4eXoiIGFuZCBpdCByZXR1cm4gdHJ1ZSBhY3R1YWxseSBpdCBzaG91bGQKPiBy
    ZXR1cm4gZmFsc2UuCj4KPiBSZWdhcmRzLAo+Cj4gLS0KPiBQb3N0ZWQgdmlhIGh0dHA6Ly93d3cu
    cnVieS1mb3J1bS5jb20vLgo+Cj4KCgotLSAK4KSF4KSt4KS/4KSc4KWA4KSkCgpbIHdyaXR0ZW4g
    aW4gaHR0cDovL3d3dy5wYWFoaWplbi5jb20vc2NyYXRjaHBhZCBdCgpbIGh0dHA6Ly93d3cucGFh
    aGlqZW4uY29tIF0K
    Abhijit Gadgil, May 4, 2007
    #7
  8. 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
    Robert Klemme, May 4, 2007
    #8
  9. 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/
    Rick DeNatale, May 4, 2007
    #9
  10. Ravi Singh

    Ravi Singh Guest

    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/.
    Ravi Singh, May 5, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,289
  2. Matt
    Replies:
    4
    Views:
    9,990
    Thomas Kellerer
    Aug 21, 2004
  3. Roger L. Cauvin

    Match First Sequence in Regular Expression?

    Roger L. Cauvin, Jan 26, 2006, in forum: Python
    Replies:
    43
    Views:
    1,137
    Armin Steinhoff
    Jan 28, 2006
  4. Jürgen Exner
    Replies:
    0
    Views:
    2,746
    Jürgen Exner
    Apr 12, 2009
  5. Peter Tuente
    Replies:
    0
    Views:
    16,890
    Peter Tuente
    Apr 17, 2009
Loading...

Share This Page