Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 2nd, 2013, 03:23 PM
MynE MynE is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 38 MynE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 18 m 33 sec
Reputation Power: 1
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

Reply With Quote
  #2  
Old March 2nd, 2013, 08:20 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
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.
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : March 2nd, 2013 at 08:22 PM.

Reply With Quote
  #3  
Old March 2nd, 2013, 10:17 PM
MynE MynE is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 38 MynE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 18 m 33 sec
Reputation Power: 1
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.

Reply With Quote
  #4  
Old March 2nd, 2013, 10:45 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
Mastering Regular Expressions by Friedl.
You could enjoy this book, or study this
Practice on the python command line.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > About re.search() question

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap