Link to home
Start Free TrialLog in
Avatar of Mytix
Mytix

asked on

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!
ASKER CERTIFIED SOLUTION
Avatar of rjkimble
rjkimble

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mytix
Mytix

ASKER

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?
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.
Avatar of Mytix

ASKER

oh...ic, well assume i dont have any attributes, i dont really need it, right?
thanks anyway!
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?
Avatar of Mytix

ASKER

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!
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.
Avatar of Mytix

ASKER

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?
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.