question on regular expressions

Discussion in 'Python' started by Darren Dale, Dec 3, 2004.

  1. Darren Dale

    Darren Dale Guest

    I'm stuck. I'm trying to make this:

    file://C:%5Cfolder1%5Cfolder2%5Cmydoc1.pdf,file://C
    %5Cfolderx%5Cfoldery%5Cmydoc2.pdf

    (no linebreaks) look like this:

    ../mydoc1.pdf,./mydoc2.pdf

    my regular expression abilities are dismal. I won't list all the
    unsuccessful things I've tried, in a nutshell, the greedy operators are
    messing me up, truncating the output to ./mydoc2.pdf. Could someone offer a
    suggestion?

    Thanks,
    Darren
     
    Darren Dale, Dec 3, 2004
    #1
    1. Advertising

  2. Darren Dale

    Sean Ross Guest

    "Darren Dale" <> wrote in message
    news:coq8n8$san$...
    > I'm stuck. I'm trying to make this:
    >
    > file://C:%5Cfolder1%5Cfolder2%5Cmydoc1.pdf,file://C
    > %5Cfolderx%5Cfoldery%5Cmydoc2.pdf
    >
    > (no linebreaks) look like this:
    >
    > ./mydoc1.pdf,./mydoc2.pdf
    >
    > my regular expression abilities are dismal. I won't list all the
    > unsuccessful things I've tried, in a nutshell, the greedy operators are
    > messing me up, truncating the output to ./mydoc2.pdf. Could someone offer

    a
    > suggestion?
    >
    > Thanks,
    > Darren


    from os.path import basename
    import urllib

    url = 'file://C:%5Cfolder1%5Cfolder2%5Cmydoc1.pdf'
    print './%s'%basename(urllib.url2pathname(url))

    HTH,
    Sean
     
    Sean Ross, Dec 3, 2004
    #2
    1. Advertising

  3. Darren Dale

    Michael Fuhr Guest

    Darren Dale <> writes:

    > I'm stuck. I'm trying to make this:
    >
    > file://C:%5Cfolder1%5Cfolder2%5Cmydoc1.pdf,file://C
    > %5Cfolderx%5Cfoldery%5Cmydoc2.pdf
    >
    > (no linebreaks) look like this:
    >
    > ./mydoc1.pdf,./mydoc2.pdf
    >
    > my regular expression abilities are dismal.


    This works for the example string you gave:

    newstring = re.sub(r'[^,]*%5[Cc]', './', examplestring)

    This replaces all instances of zero or more non-commas that are
    followed by '%5C' or '%5c' with './'. Greediness causes the pattern
    to replace everything up to the last '%5C' before a comma or the
    end of the string.

    Regular expressions aren't the only way to do what you want. Python
    has standard modules for parsing URLs and file paths -- take a look
    at urlparse, urllib/urllib2, and os.path.

    --
    Michael Fuhr
    http://www.fuhr.org/~mfuhr/
     
    Michael Fuhr, Dec 3, 2004
    #3
  4. Darren Dale

    Darren Dale Guest

    Michael Fuhr wrote:

    > Darren Dale <> writes:
    >
    >> I'm stuck. I'm trying to make this:
    >>
    >> file://C:%5Cfolder1%5Cfolder2%5Cmydoc1.pdf,file://C
    >> %5Cfolderx%5Cfoldery%5Cmydoc2.pdf
    >>
    >> (no linebreaks) look like this:
    >>
    >> ./mydoc1.pdf,./mydoc2.pdf
    >>
    >> my regular expression abilities are dismal.

    >
    > This works for the example string you gave:
    >
    > newstring = re.sub(r'[^,]*%5[Cc]', './', examplestring)
    >
    > This replaces all instances of zero or more non-commas that are
    > followed by '%5C' or '%5c' with './'. Greediness causes the pattern
    > to replace everything up to the last '%5C' before a comma or the
    > end of the string.
    >
    > Regular expressions aren't the only way to do what you want. Python
    > has standard modules for parsing URLs and file paths -- take a look
    > at urlparse, urllib/urllib2, and os.path.
    >


    Thanks to both of you. I thought re's were appropriate because the string I
    gave is buried in an xml file. A more representative example is:

    [...snip...]<url>file://C:%5Cfolder1%5Cfolder2%5Cmydoc1.pdf</url>[...snip...
    data]<url>file://C%5Cfolderx%5Cfoldery%5Cmydoc2.pdf</url>[...snip...]
     
    Darren Dale, Dec 3, 2004
    #4
    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. Jay Douglas
    Replies:
    0
    Views:
    609
    Jay Douglas
    Aug 15, 2003
  2. ASP.Confused

    Question on regular expressions

    ASP.Confused, Jul 26, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    379
    ASP.Confused
    Jul 26, 2004
  3. jeffM
    Replies:
    3
    Views:
    386
    Alan Moore
    Feb 2, 2004
  4. Replies:
    9
    Views:
    490
    Roedy Green
    Jan 23, 2006
  5. Noman Shapiro
    Replies:
    0
    Views:
    234
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page