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

Demian Brecht demianbrecht at gmail.com
Wed Feb 6 16:55:58 EST 2013


Well, an alternative /could/ be:

from urlparse import urlparse

parts = urlparse('http://alongnameofasite1234567.com/q?sports=run&a=1&b=1')
print '%s%s_%s' % (parts.netloc.replace('.', '_'),
    parts.path.replace('/', '_'),
    parts.query.replace('&', '_').replace('=', '_')
    ) 


Although with the result of:

alongnameofasite1234567_com_q_sports_run_a_1_b_1
         1288 function calls in 0.004 seconds


Compared to regex method:

498 function calls (480 primitive calls) in 0.000 seconds

I'd prefer the regex method myself.

Demian Brecht
http://demianbrecht.github.com




On 2013-02-06 1:41 PM, "rh" <richard_hubbe11 at lavabit.com> wrote:

>http://alongnameofasite1234567.com/q?sports=run&a=1&b=1





More information about the Python-list mailing list