Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

simple regex function

Posted on 2004-06-07
9
Medium Priority
?
698 Views
Last Modified: 2010-04-16
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!
0
Comment
Question by:Mytix
  • 5
  • 4
9 Comments
 
LVL 9

Accepted Solution

by:
rjkimble earned 260 total points
ID: 11247157
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
 
LVL 3

Author Comment

by:Mytix
ID: 11254658
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
 
LVL 9

Expert Comment

by:rjkimble
ID: 11256544
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
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
LVL 3

Author Comment

by:Mytix
ID: 11265934
oh...ic, well assume i dont have any attributes, i dont really need it, right?
thanks anyway!
0
 
LVL 9

Expert Comment

by:rjkimble
ID: 11265976
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
 
LVL 3

Author Comment

by:Mytix
ID: 11266004
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
 
LVL 9

Expert Comment

by:rjkimble
ID: 11266199
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
 
LVL 3

Author Comment

by:Mytix
ID: 11266808
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
 
LVL 9

Expert Comment

by:rjkimble
ID: 11267021
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question