re sub help
mwm at mired.org
Sat Nov 5 08:22:59 CET 2005
s99999999s2003 at yahoo.com writes:
> i have a string :
> a =
> inside the string, there are "\n". I don't want to substitute the '\n'
> in between
> the [startdelim] and [enddelim] to ''. I only want to get rid of the
> '\n' everywhere else.
Well, I'm not an expert on re's - I've only been using them for three
decades - but I'm not sure this can be done with a single re, as the
pattern you're interested in depends on context, and re's don't handle
On the other hand, this is fairly straightforward with simple string
>>> a = "this\nis\na\nsentence[startdelim]this\nis\nanother[enddelim]this\nis\n"
>>> sd = '[startdelim]'
>>> ed = '[enddelim]'
>>> s, r = a.split(sd, 1)
>>> m, e = r.split(ed, 1)
>>> a = s + sd + m.replace('\n', '') + ed + e
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list