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


Groups > comp.lang.python > #38312

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; "'',": 0.07; 'converts': 0.07; 'so?': 0.07; 'differently.': 0.09; 'filename,': 0.09; 'way:': 0.09; 'def': 0.10; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'lambda': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'subject:search': 0.16; 'wrote:': 0.17; 'alternate': 0.17; 'http': 0.22; 'this:': 0.23; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'i.e.': 0.27; 'replace': 0.27; 'subject:/': 0.28; 'once.': 0.29; 'becomes': 0.30; 'received:84': 0.32; 'curious': 0.33; 'to:addr:python-list': 0.33; 'done': 0.34; 'protocol': 0.35; 'there': 0.35; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'easily': 0.39; 'received:192.168': 0.40; 'remove': 0.61; 'skip:a 40': 0.61; 'more': 0.63; 'header:Reply-To:1': 0.68; 'url:a': 0.72; 'reply- to:no real name:2**0': 0.72; 'reply-to:addr:python.org': 0.84; 'subject:via': 0.84; 'step.': 0.91
X-CM-Score 0.00
X-CNFS-Analysis v=2.0 cv=HO4d4PRv c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=Tv4_tr9OjFAA:10 a=ZAt4Bo3NXscA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=cfvsAAwPeOsA:10 a=ZyT8uJY1AAAA:8 a=kdX5bpUaUi_RRMwtW1wA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117
X-AUTH mrabarnett:2500
Date Wed, 06 Feb 2013 23:11:57 +0000
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130107 Thunderbird/17.0.2
MIME-Version 1.0
To python-list@python.org
Subject Re: Curious to see alternate approach on a search/replace via regex
References <20130206134105.9352d665e1f8d0719021fcf9@lavabit.com>
In-Reply-To <20130206134105.9352d665e1f8d0719021fcf9@lavabit.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To python-list@python.org
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1430.1360192315.2939.python-list@python.org> (permalink)
Lines 31
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1360192315 news.xs4all.nl 6879 [2001:888:2000:d::a6]:60110
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38312

Show key headers only | View raw


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)

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web