Regex for Floats

For the first part, you are to develop a regular expression to recognize floating point numbers of the form described here. I would recommend doing this in a piece by piece fashion using scalar variables to store different subexpressions of the final regular expression. Store your Perl script in float.plx.

For our purposes, a floating point number consists of

1.
an + or - (optional - one or neither of them can be present), followed by
2.
one or more digits, followed by
3.
a decimal part, followed by
4.
an exponent part (optional - need not be present to match)

The decimal part consists of

1.
a decimal point, followed by
2.
one or more digits

The exponent part consists of

1.
the letter e or E, followed by
2.
an + or - (optional - one or neither of them can be present), followed by
3.
one or more digits

The following are examples of floating point numbers that would be matched based on the above rules:

6.35   -3.6   +5.4   5.0e1   2.5E+2   4.4e-10   0.5

The following are examples of substrings that would not be matched based on the above rules:

abc   3-e6   5+.5   6   5.  .5   5e1
Take note of the last four. Integers should not be matched. And while the last 3 are valid in most programming languages, the rules given above exclude them to simplify things slightly.


next up
Next: Library Catalog Search Up: Project 1

Moorthy
5/28/1998