change a string conditional

obantec support spam at nomail.net
Mon May 13 07:48:07 EDT 2002


"Harvey Thomas" <hst at empolis.co.uk> wrote in message
news:mailman.1020965239.20491.python-list at python.org...

> Mark wrote
> > Hi
> >
> > I need to a piece of python code that will take a string
> > which may or may
> > not begin with www and change it to lists.
> >
> > eg. www.domain.com becomes lists.domain.com
> > but if domain1.com (no www) the it still becomes lists.domain1.com
> >
> > an example of a pattern match will be enough.(I do a bit of
> > perl but never
> > done any python)
> >
> > mark
> >
>
> Assuming that the string astring is not empty (if it is then
> newstring will be empty), then
>
> newstring = re.sub('(www\.)?(.+)', lambda x:'www.%s' %
> x.group(2), astring)
>
> should do the trick
>
>
Sorry, the example should of course be:
newstring = re.sub('(www\.)?(.+)', lambda x:'lists.%s' % x.group(2),
astring)

_____________________________________________________________________
This message has been checked for all known viruses by the MessageLabs Virus
Scanning Service.

Thanks Harvey, i went with this and it works great now i got my mind round
python's pedantic syntax
(spotted the lists / www error!)

Mark







More information about the Python-list mailing list