Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38312 > unrolled thread

Re: Curious to see alternate approach on a search/replace via regex

Started byMRAB <python@mrabarnett.plus.com>
First post2013-02-06 23:11 +0000
Last post2013-02-06 23:11 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Curious to see alternate approach on a search/replace via regex MRAB <python@mrabarnett.plus.com> - 2013-02-06 23:11 +0000

#38312 — Re: Curious to see alternate approach on a search/replace via regex

FromMRAB <python@mrabarnett.plus.com>
Date2013-02-06 23:11 +0000
SubjectRe: Curious to see alternate approach on a search/replace via regex
Message-ID<mailman.1430.1360192315.2939.python-list@python.org>
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.
>
> This:
>
> http://alongnameofasite1234567.com/q?sports=run&a=1&b=1
>
> becomes this:
>
> alongnameofasite1234567_com_q_sports_run_a_1_b_1
>
>
> 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:

def u2f(u):
     return re.sub(r'^https?://|([-:./?&=]+)', lambda m: '_' if 
m.group(1) else '', u)

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web