simple regex function

hi, i have a string "my name is <name>smith</name>. what is yours?"
sort of like in an XML format, i need to capture smith by identifying the <name> tags. is there a way to get it beside using XML parsers (i.e. simple regex)? if it is, how do i capture it in python?
thanks!
LVL 3
MytixAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rjkimbleCommented:
Here's a short script for illustration:

import re
reName = re.compile( r'<name.*?>(.*?)</name>' )
s = 'my name is <name>smith</name>. what is yours?'
m = reName.search( s )
if m:
    print m.group( 1 )

Not much to it, really.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MytixAuthor Commented:
ok..quick question about re.compile( r'<name.*?>(.*?)</name>' )
what is the .*? used for after name? i dont think its needed, but why is it there?
0
rjkimbleCommented:
If the <name> tag is always just that, then you're right. I was allowing for the possibility that you might have some attributes like this:

<name style="somestyle" type="sometype">smith</name>

That's all. It does no harm, and it handles a slightly more general case.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

MytixAuthor Commented:
oh...ic, well assume i dont have any attributes, i dont really need it, right?
thanks anyway!
0
rjkimbleCommented:
You don't need the ".*?" in <name.*?>. Under your stated conditions, both work the same.

Do you have any more questions? I think this one is about played out, don't you agree?
0
MytixAuthor Commented:
and if i may, ask another question, if its too hard, dont bother. its just out of curiosity.
If i have a python file/script, and i would like to find the contents of a certain function in the file (i.e. its code), is it possible? i got the function name given the frame using:

name = frame.f_code.co_name

(note, the original class inherits bdb). the problem is that the function is not called at all from the script, thus i cant use
fn = self.canonic(frame.f_code.co_filename)            
line = linecache.getline(fn, frame.f_lineno).strip()
to get the lines.
any ideas?
thanks!
0
rjkimbleCommented:
That one's a bit beyond me. I don't follow what you have done there. I'm not even sure what you're asking exactly.
0
MytixAuthor Commented:
Say i have a file test.py and a name of a function func, is it possible to get the contents/code of the function into say a list or an object? does anyone else know how i might acomplish this without regular expressions?
0
rjkimbleCommented:
I think you should read chapter 18 in the Python Library Reference. It seems that Python comes with all kinds of support for doing the kinds of things you're interested in doing, but I'm not sure how much work is required for you accomplish what you want.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.