Re: Curious to see alternate approach on a search/replace via regex
On 2013-02-06 21:41, rh wrote:
> I am curious to know if others would have done this differently. And if so
> how so?
> This converts a url to a more easily managed filename, stripping the
> http protocol off.
> becomes this:
> def u2f(u):
> nx = re.compile(r'https?://(.+)$')
> u = nx.search(u).group(1)
> ux = re.compile(r'([-:./?&=]+)')
> return ux.sub('_', u)
> One alternate is to not do the compile step. There must also be a way to
> do it all at once. i.e. remove the protocol and replace the chars.
Here's one way:
return re.sub(r'^https?://|([-:./?&=]+)', lambda m: '_' if
m.group(1) else '', u)
|All times are GMT. The time now is 08:44 PM.|
Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.