[Tutor] regular expressions

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Jul 18 07:08:25 CEST 2005



On Sun, 17 Jul 2005, Servando Garcia wrote:

> Using regular expressions how do I represent Floats.

Hi Servando,

I know that you're working on a parser project, but the regex for doing
floats is pretty much a homework project.  There's not much we can do
except point you toward the Regular Expression HOWTO tutorial page:

    http://www.amk.ca/python/howto/regex/

I'd also strongly recommend that you look at the documentation in the
Python Standard Library, as it does have concrete examples on how to
recognize things like floats here:

    http://www.python.org/doc/lib/node117.html


Your question is also context-sensitive: what a floating point number
looks like really depends on what kind of language we're modeling.

As a concrete example, in the ML family of languages:

    .42

is not a floating-point literal, although it is one in Python, because
it's missing a leading decimal digit.  Conversely, in the Ruby language,

    123_456.789

is a floating point literal, although it isn't one in Python, because
Python doesn't allow underscores.  I guess I'm trying to say: some lexical
conventions are not universal:  otherwise, things would be too easy.
*grin*

You can't assume that everyone uses the same notation for floating point
literals: you need to show examples of what you consider to be floats.
If you want to see Python's specification for floating point literals,
see:

    http://www.python.org/doc/ref/floating.html



More information about the Tutor mailing list