Question about parsing a string

A

Alex Martelli

Nico Grubert said:
Hi there,

I would like to parse a string in Python.

If the string is e.g. 'http://www.whatever.org' I would like
to generate this string:
'<a href="http://www.whatever.org">http://www.whatever.org</a>'

If the string is e.g. 'My link' I
would like to generate this string:
'<a href="http://www.whatever.org">My link</a>'

Any idea how I can do this? Maybe with regular expressions?

If you know the string always starts with '')
url = s[_startlen:where_closebracket]
txt = s[where_closebracket+1:-_endlen]
if not txt: txt = url
return '<a href="%s">%s</a>' % (url, txt)

I've just typed in this code without trying it out, but roughly it
should be what you want.


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,326
Members
48,013
Latest member
fmb_amith

Latest Threads

Top