Thread: help with regex

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    66
    Rep Power
    13

    help with regex


    I have a string that needs to be match by several different regular expressions to see which one matches it. I'm trying to figure out the best way to do this (I'm a python nub).

    In pseudo-code:

    Code:
    if ( mystring matches regex_1 ) : 
     # do some stuff
    elif ( string matches regex_2 ) :
     # do something else
    elif ( string matches regex_3 ) :
     # do something else
    else :
     # who cares
    The thing I don't understand is that I can't do an assignment inside the if argument.

    For instance, I can't do this:
    Code:
    if (m = re.match('(.*)', text)
        print m.group(1)
    So how do I go about extracting the group, if I can't assign re.match to anything in the if statement?

    Thanks in advance,
    Justin
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65

    Smile


    You have to put something in the if statement that evaluates to True or False!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    66
    Rep Power
    13
    Yah, I know. How do I go about getting the regex statement to match AND return true/false at the same time?
  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    You could wrap the test in a function and use the function in your if statement. Or - you could go the whole hog and create a whole new class that might be generally more useful. Here's the start of it...

    python Code:
    import re
     
    class reObj:
        def __init__(self,pattern,flags=0):
            self.pattern = re.compile(pattern,flags)
            self.result = None
     
        def mtest(self,tstr):
            self.result = self.pattern.match(tstr)
            if self.result == None:
                return False
            return True
     
        def group(self,item):
            return self.result.group(item)
     
     
    if __name__ == '__main__':
        recase = reObj('(aa+)')
     
        if recase.mtest("bbbb"):
            print recase.group(0)
        elif recase.mtest(""):
            print recase.group(0)
        elif recase.mtest("a"):
            print recase.group(0)
        elif recase.mtest("aaaa"):
            print recase.group(0)


    If this is too much then consider restructuring your code rather than trying to make Python fit C.

    grim
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65

    Smile


    This will work:
    Code:
    import re
    
    text = "Ursula and Monika are absolutely cute!"
    
    if re.match('(.*)(Jean)', text):
        print "Jean is cute!"
    elif re.match('(.*)(Monika)', text):
        print "Monika is cute!"
    else:
        print "It's an ugly world out there!"
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    yes and just as a note regex is out dated we use re
    ... as you can see above
    Those people who think they know everything are a great annoyance to those of us who do.

IMN logo majestic logo threadwatch logo seochat tools logo