Question about parsing a string

Alex Martelli aleax at
Mon Oct 10 10:27:49 CEST 2005

Nico Grubert <nicogrubert at> wrote:

> Hi there,
> I would like to parse a string in Python.
> If the string is e.g. '[url=][/url]' I would like
> to generate this string:
> '<a href=""></a>'
> If the string is e.g. '[url=]My link[/url]' I 
> would like to generate this string:
> '<a href="">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.


More information about the Python-list mailing list