quick regex question

James Stroud jstroud at mbi.ucla.edu
Thu Oct 28 16:11:48 EDT 2004


> > >> "".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	    matt.price at utoronto.ca
> History Department, University of Toronto
> (416) 978-2094
> --------------------------------------------
>
> please don't use the following addresses:  zeus at derailleur.org
> aardvark at derailleur.org

-- 
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/



More information about the Python-list mailing list