change a string conditional

Harvey Thomas hst at empolis.co.uk
Thu May 9 13:20:20 EDT 2002


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


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





More information about the Python-list mailing list