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

    Join Date
    Nov 2012
    Posts
    42
    Rep Power
    2

    About re.search() question


    Hi, I have a question with the code below about re() module

    phrase = 'Les't play my music now'

    m = (re.search(("play"), phrase) and re.search(("music"), phrase))

    k = m.group(0)
    print k // I got only one word 'music'

    I'd like to capture two words which are 'play' and 'music', can someone please tell me how can I do that?

    by 'k' should have an out put like this 'play','music'. please help me find out. Thank you
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    Would wider spacing help you see?
    Code:
    m = re.search("play",phrase)      and        re.search("music",phrase)
    The result of a python expression involving `and' and `or' logical tests is the last object python needed to evaluate to resolve the condition. Python evaluates from left to right, and obeying parentheses.

    Thus 131 is the result of

    131 or 'Our cat Zippy'

    Try it in the interpreter!


    In your case
    Code:
    re.search("play",phrase) and re.search("music",phrase)
    the search for 'play' succeeded so the match object tested True, so python evaluated
    re.search("music",phrase)
    Since there's nothing left to evaluate python returns the object that determined the outcome of the `and' expression and assigns it to m.
    Last edited by b49P23TIvg; March 2nd, 2013 at 08:22 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    42
    Rep Power
    2
    Oh I thank you, because of you I've got some ideas, here's what I have now:

    Code:
    phrase = "Lets play my music"
    
    j = re.search(("play"), phrase)
    m =  re.search(("music"), phrase)
    if m and j:
           try:
              k = m.group(0)
              l = j.group(0)
              phrase = l+' '+k
              print phrase  // result is 'play music'
           except AttributeError:
              print "No match found"
    I'd like to ask you that what if I want to search multiple strings at the same time, is it possible to do that? for example like:

    j = re.search(("play" || "open"), phrase)
    m = re.search(("music" || "song"), phrase)

    Is it possible to do something like this in only one line? i'd appreciate your answer very much. Thank you so much.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    Mastering Regular Expressions by Friedl.
    You could enjoy this book, or study this
    Practice on the python command line.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo