String + number split
Graham Breed
usenet at microtonal.co.uk
Mon Apr 12 16:23:58 EDT 2004
Stevie_mac wrote:
> 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.
Yes, certainly. Well, where to go would be the standard documentation,
which you can now get from the command line:
>>> import sre
>>> help(sre)
Help on module sre:
...
but in this case, the expression is
\d+$|_
\d matches a digit
+ means match more than 1 digit
$ ensures the number is at the end of the string
| means either \d+$ or _ can match
_ is a literal underscore
All I did then is substitute any matches with an empty string, which is
the same as deleting all matches. Then (on the original string) return
the last match, which will be the number at the end, and convert it to
an integer. If the string isn't in the format you said it would be,
you'll get either an IndexError or a ValueError.
What your code was actually doing is subtly different -- assuming an
uninterrupted string of letters exists at the front of the string, and
anything that isn't alphabetic before the number starts. The other
things people are suggesting are better tailored for this problem. The
expression(s) you should use depend on the exact problem you're trying
to solve.
Graham
More information about the Python-list
mailing list