Nico Grubert <[EMAIL PROTECTED]> 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 -- http://mail.python.org/mailman/listinfo/python-list