Re: quick regex question

Discussion in 'Python' started by Steven Bethard, Oct 28, 2004.

  1. Matt Price <matt.price <at> utoronto.ca> writes:
    >
    > this is surely trivial, but can'f figure it out. I wnat to replace:
    >
    > 'string with spaces'
    > with
    > 'StringWithSpaces'



    I know you asked for a regex solution, but I thought I'd drop this suggestion
    anyway (not everything really merits a regex) =)

    >>> string1 = 'string with spaces'
    >>> ''.join([s.title() for s in string1.split()])

    'StringWithSpaces'

    Clear, concise, and uses in the builtin str.title method, which appears to do
    exactly what you want.

    Steve
    Steven Bethard, Oct 28, 2004
    #1
    1. Advertising

  2. Steven Bethard

    Peter Otten Guest

    Steven Bethard wrote:

    > Matt Price <matt.price <at> utoronto.ca> writes:
    >>
    >> this is surely trivial, but can'f figure it out. I wnat to replace:
    >>
    >> 'string with spaces'
    >> with
    >> 'StringWithSpaces'

    >
    >
    > I know you asked for a regex solution, but I thought I'd drop this
    > suggestion anyway (not everything really merits a regex) =)


    I second that :)

    >>>> string1 = 'string with spaces'
    >>>> ''.join([s.title() for s in string1.split()])

    > 'StringWithSpaces'
    >


    Or, without the list comprehension:

    >>> "".join("string with spaces".title().split())

    'StringWithSpaces'

    > Clear, concise, and uses in the builtin str.title method, which appears to
    > do exactly what you want.


    Peter
    Peter Otten, Oct 28, 2004
    #2
    1. Advertising

  3. Steven Bethard

    Matt Price Guest

    On Thu, Oct 28, 2004 at 08:23:23PM +0200, Peter Otten wrote:
    > Steven Bethard wrote:
    >
    > > Matt Price <matt.price <at> utoronto.ca> writes:
    > >>

    > >
    > > I know you asked for a regex solution, but I thought I'd drop this
    > > suggestion anyway (not everything really merits a regex) =)

    >
    > I second that :)
    >
    > >>>> string1 = 'string with spaces'
    > >>>> ''.join([s.title() for s in string1.split()])

    > > 'StringWithSpaces'
    > >

    > Or, without the list comprehension:
    >
    > >>> "".join("string with spaces".title().split())

    > 'StringWithSpaces'


    wow, that is really elegant. thank you both.

    matt

    -------------------------------------------
    Matt Price
    History Department, University of Toronto
    (416) 978-2094
    --------------------------------------------

    please don't use the following addresses:
    Matt Price, Oct 28, 2004
    #3
  4. Steven Bethard

    James Stroud Guest

    > > >> "".join("string with spaces".title().split())
    > >
    > > 'StringWithSpaces'

    >
    > wow, that is really elegant. thank you both.


    Yes, elegant.

    > > suggestion anyway (not everything really merits a regex) =)


    Unfortunately true. However, as regex is fun...

    s/(^|\s)+(\S)/\u$2/g

    (Perl. I think the translation to Python is trivial...but not as unreadable.)

    James

    On Thursday 28 October 2004 12:28 pm, Matt Price wrote:
    > On Thu, Oct 28, 2004 at 08:23:23PM +0200, Peter Otten wrote:
    > > Steven Bethard wrote:
    > > > Matt Price <matt.price <at> utoronto.ca> writes:
    > > >
    > > >
    > > > I know you asked for a regex solution, but I thought I'd drop this
    > > > suggestion anyway (not everything really merits a regex) =)

    > >
    > > I second that :)
    > >
    > > >>>> string1 = 'string with spaces'
    > > >>>> ''.join([s.title() for s in string1.split()])
    > > >
    > > > 'StringWithSpaces'

    > >
    > > Or, without the list comprehension:
    > > >>> "".join("string with spaces".title().split())

    > >
    > > 'StringWithSpaces'

    >
    > wow, that is really elegant. thank you both.
    >
    > matt
    >
    > -------------------------------------------
    > Matt Price
    > History Department, University of Toronto
    > (416) 978-2094
    > --------------------------------------------
    >
    > please don't use the following addresses:
    >


    --
    James Stroud, Ph.D.
    UCLA-DOE Institute for Genomics and Proteomics
    611 Charles E. Young Dr. S.
    MBI 205, UCLA 951570
    Los Angeles CA 90095-1570
    http://www.jamesstroud.com/
    James Stroud, Oct 28, 2004
    #4
  5. Peter Otten <> wrote:
    > Steven Bethard wrote:
    > > Matt Price <matt.price <at> utoronto.ca> writes:
    > > > this is surely trivial, but can'f figure it out. I wnat to replace:
    > > >
    > > > 'string with spaces'
    > > > with
    > > > 'StringWithSpaces'

    > >
    > > I know you asked for a regex solution, but I thought I'd drop this
    > > suggestion anyway (not everything really merits a regex) =)

    >
    > I second that :)


    Me too. :)

    > > >>> string1 = 'string with spaces'
    > > >>> ''.join([s.title() for s in string1.split()])

    > > 'StringWithSpaces'

    >
    > Or, without the list comprehension:
    >
    > >>> "".join("string with spaces".title().split())

    > 'StringWithSpaces'
    >
    > > Clear, concise, and uses in the builtin str.title method, which appears to
    > > do exactly what you want.


    Uhm, I think the following is more efficient:

    >>> "string with spaces".title().replace(" ", "")

    'StringWithSpaces'

    It doesn't split into a temporary list of strings which
    are then joined together again. Also, I believe it's more
    readable than the split/join approach.

    Best regards
    Oliver

    --
    Oliver Fromme, Konrad-Celtis-Str. 72, 81369 Munich, Germany

    ``All that we see or seem is just a dream within a dream.''
    (E. A. Poe)
    Oliver Fromme, Oct 29, 2004
    #5
  6. Steven Bethard

    Peter Otten Guest

    Oliver Fromme wrote:

    > > > >>> string1 = 'string with spaces'
    > > > >>> ''.join([s.title() for s in string1.split()])
    > > > 'StringWithSpaces'

    > >
    > > Or, without the list comprehension:
    > >
    > > >>> "".join("string with spaces".title().split())

    > > 'StringWithSpaces'
    > >
    > > > Clear, concise, and uses in the builtin str.title method, which
    > > > appears to do exactly what you want.

    >
    > Uhm, I think the following is more efficient:
    >
    >>>> "string with spaces".title().replace(" ", "")

    > 'StringWithSpaces'
    >
    > It doesn't split into a temporary list of strings which
    > are then joined together again. Also, I believe it's more
    > readable than the split/join approach.


    You win :)

    Peter
    Peter Otten, Oct 29, 2004
    #6
    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. JKop
    Replies:
    11
    Views:
    852
  2. Matt Price

    quick regex question

    Matt Price, Oct 28, 2004, in forum: Python
    Replies:
    0
    Views:
    622
    Matt Price
    Oct 28, 2004
  3. Robert Brewer

    RE: quick regex question

    Robert Brewer, Oct 28, 2004, in forum: Python
    Replies:
    0
    Views:
    626
    Robert Brewer
    Oct 28, 2004
  4. Replies:
    3
    Views:
    726
    Reedick, Andrew
    Jul 1, 2008
  5. Jürgen Exner

    Re: Quick perl script-regex question

    Jürgen Exner, Nov 4, 2011, in forum: Perl
    Replies:
    0
    Views:
    1,955
    Jürgen Exner
    Nov 4, 2011
Loading...

Share This Page