Question about parsing a string

Alex Martelli aleax at mail.comcast.net
Mon Oct 10 04:27:49 EDT 2005


Nico Grubert <nicogrubert at gmail.com> wrote:

> Hi there,
> 
> I would like to parse a string in Python.
> 
> If the string is e.g. '[url=http://www.whatever.org][/url]' I would like
> to generate this string:
> '<a href="http://www.whatever.org">http://www.whatever.org</a>'
> 
> If the string is e.g. '[url=http://www.whatever.org]My link[/url]' 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=' and ends with '[/url]'
(or, any string not thus starting/ending are to be skipped, etc), REs
are a bit of an overkill (they'll work, but you can do it more simply).

If your actual needs are different, you'll have to express them more
explicitly.  But assuming the "given starting and ending" scenario:

_start = '[url='
_startlen = len(_start)
_end = '[/url]'
_endlen = len(_end)
def doit(s):
  if s[:_startlen] != _start: raise ValueError
  if s[-_endlen:] != _end: raise ValueError
  where_closebracket = s.index(']')
  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



More information about the Python-list mailing list