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

Discussion in 'Python' started by MRAB, Feb 6, 2013.

  1. MRAB

    MRAB Guest

    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)
    MRAB, Feb 6, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rh
    Replies:
    6
    Views:
    114
    Nick Mellor
    Feb 8, 2013
  2. Demian Brecht
    Replies:
    18
    Views:
    208
  3. Demian Brecht
    Replies:
    0
    Views:
    119
    Demian Brecht
    Feb 6, 2013
  4. Peter Otten
    Replies:
    0
    Views:
    78
    Peter Otten
    Feb 7, 2013
  5. Demian Brecht
    Replies:
    0
    Views:
    75
    Demian Brecht
    Feb 7, 2013
Loading...

Share This Page