regular expression for integer and decimal numbers

Andrew Durdin adurdin at gmail.com
Thu Sep 23 21:15:48 EDT 2004


On 23 Sep 2004 17:51:17 -0700, gary <gary.wilson at gmail.com> wrote:
> I want to pick all intergers and decimal numbers out of a string.
> Would this be the most correct regular expression to use?
> 
> "\d+\.?\d*"

That will work for numbers such as 0123  12.345  12.  0.5 -- but it
won't work for the following:
0x12AB  .5  10e-3  -15  123L
If you want to handle some of those, then you'll need a more complicated regex.
If you want to accept numbers of the form .5 but don't care about 12.
then a better regex would be
    \d*\.?\d+



More information about the Python-list mailing list