String + number split

Stevie_mac no.email at please.com
Mon Apr 12 15:13:09 EDT 2004


now thats what im talking about!
note to self, learn reg exp!

Thing is, dont understand it one bit! any chance you comment it!  (tell me where to go if you want!) I'd appreciate it
if you could - ta.

"Graham Breed" <usenet at microtonal.co.uk> wrote in message news:YjBec.70215$Id.61871 at news-binary.blueyonder.co.uk...
> Stevie_mac wrote:
>
> > Hello again, I can do this, but I'm sure there is a much more elegant way...
> >
> > A string, with a number on the end, strip off the number & discard any underscores
> >
> > eg...
> >
> > font12        becomes         ('font',12)
> > arial_14      becomes        ('arial',14)
>
> Hmm, well, there's always
>
>  >>> import re
>  >>> def fsplit(s):
> matcher = re.compile('\d+$|_')
> return matcher.sub('', s), int(matcher.findall(s)[-1])
>
>  >>> fsplit('font12')
> ('font', 12)
>  >>> fsplit('arial14')
> ('arial', 14)
>  >>> fsplit('__arial__bold_1_14')
> ('arialbold1', 14)
>
> I'm scary enough that I probably would do it this way.
>
>
>                 Graham





More information about the Python-list mailing list