#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    1
    Rep Power
    0

    regular expression: how to fail on double characters?


    say i have 'hello'
    and i want to it to fail when it matches double l, (2 or more actually).

    any ideas?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    325
    Rep Power
    12
    import re
    str1 = "this is crazy"
    str2 = "thiis is crazy"
    if re.search('i{2,}',str1) is not None: print "first"
    if re.search('i{2,}',str2) is not None: print "second"


    the '{a,b}' specifies it is looking for between a and b occurences of it. If you leave out the b, it looks for minimum of a occurneces. Similarly with leaving out the a
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Lazy's regex will work fine, but it requires that you know what letter will occure twice (or more) before hand.. which can be very restricting! lucky you can match any letter occuring twice using backreferances with no more effort ..

    This small Python shell session should work:

    >>> import re
    >>> string = 'hello'
    >>> re.search('([a-z])\\1', string.lower()) != None
    True
    >>> string = 'hi there'
    >>> re.search('([a-z])\\1', string.lower()) != None
    False
    >>>

    Note: because of the lower() method this is effectivly a case insentative match.

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo