Regular expression, "except end of string", question.

Discussion in 'Python' started by Derek Basch, Jun 17, 2004.

  1. Derek Basch

    Derek Basch Guest

    Hello,

    I have a string like:

    string = "WHITE/CLARET/PINK/XL"

    which I need to alter to this format:

    string = "WHITE-CLARET-PINK/XL"

    I developed the below functions to do so. However, something is wrong
    with my regular expression. It currently matches with the following:

    /CLARET
    /PINK
    /XL

    I thought perhaps I could exclude the match that includes the end of the
    string with someting like this:

    r"([/]\w+[^$])"

    but that didnt work either. Can anyone tell me how to exclude the last
    match? The one with the end of the string "/XL".

    Thanks a million,
    Derek Basch

    ------------------------------------------------
    import re

    string = "WHITE/CLARET/PINK/XL"

    def replace_fs(thematch):
    thematch = thematch.group(1)
    thematch = "-" + thematch[1:]
    return thematch

    bs_regex = re.compile(r"([/]\w+)").sub(replace_fs, str(string))
    print bs_regex
     
    Derek Basch, Jun 17, 2004
    #1
    1. Advertising

  2. In article <>,
    Derek Basch <> wrote:

    > string = "WHITE/CLARET/PINK/XL"
    >
    > which I need to alter to this format:
    >
    > string = "WHITE-CLARET-PINK/XL"


    Do you really need regexps for this?

    >>> string = "WHITE/CLARET/PINK/XL"
    >>> '-'.join(string.split('/',2))

    'WHITE-CLARET-PINK/XL'

    --
    David Eppstein http://www.ics.uci.edu/~eppstein/
    Univ. of California, Irvine, School of Information & Computer Science
     
    David Eppstein, Jun 17, 2004
    #2
    1. Advertising

  3. Derek Basch

    Peter Hansen Guest

    Derek Basch wrote:

    > I have a string like:
    >
    > string = "WHITE/CLARET/PINK/XL"
    >
    > which I need to alter to this format:
    >
    > string = "WHITE-CLARET-PINK/XL"
    >
    > I developed the below functions to do so. However, something is wrong
    > with my regular expression.


    As they say, if you have a problem and think a regular expression
    is the best way to solve it, you might now have two problems...

    Do you always want to exclude the last "/" ? How about this
    instead:

    s = 'white/claret/pink/xl'

    s2 = s.split('/') # temporary list

    # join all but last with hyphens, add last after slash
    s = '-'.join(s2[:-1]) + '/' + s2[-1]

    # s is now 'white-claret-pink/xl'

    Depending on what the format really is (e.g. is the /XL
    actually optional?) it might be simpler or harder than
    this.

    An re can be made to work too, probably, but perhaps it
    won't be very clear.

    -Peter
     
    Peter Hansen, Jun 17, 2004
    #3
  4. Derek Basch

    Peter Hansen Guest

    David Eppstein wrote:

    > Do you really need regexps for this?
    >
    >>>>string = "WHITE/CLARET/PINK/XL"
    >>>>'-'.join(string.split('/',2))

    >
    > 'WHITE-CLARET-PINK/XL'


    Dang! I started with split('/', 2) but stared at the
    result trying to figure out how the heck to join it
    to get the right result and ended up punting. :-(

    -Peter
     
    Peter Hansen, Jun 17, 2004
    #4
  5. Derek Basch

    Derek Basch Guest

    In article <>,
    says...
    > In article <>,
    > Derek Basch <> wrote:
    >
    > > string = "WHITE/CLARET/PINK/XL"
    > >
    > > which I need to alter to this format:
    > >
    > > string = "WHITE-CLARET-PINK/XL"

    >
    > Do you really need regexps for this?
    >
    > >>> string = "WHITE/CLARET/PINK/XL"
    > >>> '-'.join(string.split('/',2))

    > 'WHITE-CLARET-PINK/XL'
    >
    >


    Thanks Peter and David,

    It always comforting to know that a really simple solution exists after
    you waste a couple of hours messing with Regular Expressions. :)

    Cheers,
    Derek
     
    Derek Basch, Jun 17, 2004
    #5
  6. Derek Basch

    David Fisher Guest

    Peter Hansen <> writes:

    > David Eppstein wrote:
    >
    > > Do you really need regexps for this?
    > >
    > >>>>string = "WHITE/CLARET/PINK/XL"
    > >>>>'-'.join(string.split('/',2))

    > > 'WHITE-CLARET-PINK/XL'

    >
    > Dang! I started with split('/', 2) but stared at the
    > result trying to figure out how the heck to join it
    > to get the right result and ended up punting. :-(
    >
    > -Peter


    $ python
    Python 2.3.4 (#1, Jun 13 2004, 11:21:03)
    [GCC 3.3.1 (cygming special)] on cygwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> string = "WHITE/CLARET/PINK/XL"
    >>> string.replace('/','-',2)

    'WHITE-CLARET-PINK/XL'
    >>>



    ><{{{*>
     
    David Fisher, Jun 17, 2004
    #6
  7. Derek Basch

    Doug Holton Guest

    Derek Basch wrote:
    >string = "WHITE/CLARET/PINK/XL"
    >
    >which I need to alter to this format:
    >
    >string = "WHITE-CLARET-PINK/XL"


    You need the (?!...) operator: http://docs.python.org/lib/re-syntax.html

    import re
    #replace "/" with "-" if not followed by a word at the end of the string
    print re.sub(r"/(?!\w+$)",r"-",s)

    but like they said, join and split are better here instead of
    using regular expressions.
     
    Doug Holton, Jun 17, 2004
    #7
  8. Derek Basch

    Peter Hansen Guest

    Derek Basch wrote:

    > Thanks Peter and David,
    >
    > It always comforting to know that a really simple solution exists after
    > you waste a couple of hours messing with Regular Expressions. :)


    And, usually, there's an even simpler solution than the first
    couple of responses, as David #2 showed. :) His has the added
    advantage of not building a temporary list too.

    -Peter
     
    Peter Hansen, Jun 17, 2004
    #8
  9. Derek Basch

    Aahz Guest

    In article <>,
    Derek Basch <> wrote:
    >
    >It always comforting to know that a really simple solution exists after
    >you waste a couple of hours messing with Regular Expressions. :)


    'Some people, when confronted with a problem, think "I know, I'll use
    regular expressions." Now they have two problems.' --Jamie Zawinski
    --
    Aahz () <*> http://www.pythoncraft.com/

    "Typing is cheap. Thinking is expensive." --Roy Smith, c.l.py
     
    Aahz, Jun 17, 2004
    #9
  10. Derek Basch

    Derek Basch Guest

    In article <caqvac$t1s$>, says...
    > In article <>,
    > Derek Basch <> wrote:
    > >
    > >It always comforting to know that a really simple solution exists after
    > >you waste a couple of hours messing with Regular Expressions. :)

    >
    > 'Some people, when confronted with a problem, think "I know, I'll use
    > regular expressions." Now they have two problems.' --Jamie Zawinski
    >


    (?!...)
    Matches if ... doesn't match next. This is a negative lookahead
    assertion. For example, Isaac (?!Asimov) will match 'Isaac ' only if
    it's not followed by 'Asimov'.

    That wasn't covered in my trusty (outdated) Python 2.1 Bible. I should
    have gone straight to the Library reference. Thanks.
     
    Derek Basch, Jun 17, 2004
    #10
  11. Derek Basch

    Peter Otten Guest

    Peter Hansen wrote:

    > David Eppstein wrote:
    >
    >> Do you really need regexps for this?
    >>
    >>>>>string = "WHITE/CLARET/PINK/XL"
    >>>>>'-'.join(string.split('/',2))

    >>
    >> 'WHITE-CLARET-PINK/XL'

    >
    > Dang! I started with split('/', 2) but stared at the
    > result trying to figure out how the heck to join it
    > to get the right result and ended up punting. :-(


    Strictly speaking the problem is underspecified, and your original solution
    might still be the best if claret is out tomorrow.

    Peter
     
    Peter Otten, Jun 17, 2004
    #11
    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. Andrew Munn

    Regular expression question...

    Andrew Munn, Jun 29, 2003, in forum: Perl
    Replies:
    1
    Views:
    2,177
    rakesh sharma
    Jun 30, 2003
  2. Glenn Kidd

    Regular expression question

    Glenn Kidd, Aug 18, 2003, in forum: Perl
    Replies:
    0
    Views:
    947
    Glenn Kidd
    Aug 18, 2003
  3. VSK
    Replies:
    2
    Views:
    2,390
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    884
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    12,051
    vforvikash
    Dec 29, 2008
Loading...

Share This Page