The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
About re.search() question
Discuss About re.search() question in the Python Programming forum on Dev Shed. About re.search() question Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 2nd, 2013, 03:23 PM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 38
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 
|

March 2nd, 2013, 08:20 PM
|
 |
Contributing User
|
|
|
|
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.
|

March 2nd, 2013, 10:17 PM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 38
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.
|

March 2nd, 2013, 10:45 PM
|
 |
Contributing User
|
|
|
|
Mastering Regular Expressions by Friedl.
You could enjoy this book, or study this
Practice on the python command line.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|