[Tutor] Regex not working as desired
Steven D'Aprano
steve at pearwood.info
Tue Feb 27 03:44:41 EST 2018
On Mon, Feb 26, 2018 at 11:01:49AM -0800, Roger Lea Scherer wrote:
> The first step is to input data and then I want to check to make sure
> there are only digits and no other type of characters. I thought regex
> would be great for this.
I'm going to quote Jamie Zawinski:
Some people, when confronted with a problem, think "I know,
I'll use regular expressions." Now they have two problems.
Welcome to the club of people who discovered that regexes are just as
likely to make things worse as better :-(
Here's another, simpler way to check for all digits:
value = '12345' # for example
value.isdigit()
The isdigit() method will return True if value contains nothing but
digits (or the empty string), and False otherwise.
Sounds like just what you want, right? Nope. It *seems* good right up to
the moment you enter a negative number:
py> '-123'.isdigit()
False
Or you want a number including a decimal point. Floating point numbers
are *especially* tricky to test for, as you have to include:
# mantissa
optional + or - sign
zero or more digits
optional decimal point (but no more than one!)
zero or more digits
but at least one digit either before or after the decimal point;
# optional exponent
E or e
optional + or - sign
one or more digits
It is hard to write a regex to match floats.
Which brings us to a better tactic for ensuring that values are a valid
int or float: try it and see!
Instead of using the Look Before You Leap tactic:
if string looks like an int:
number = int(string) # hope this works, if not, we're in trouble!
else:
handle the invalid input
we can use the "Easier To Ask For Forgiveness Than Permission" tactic,
and just *try* converting it, and deal with it if it fails:
try:
number = int(string)
except ValueError:
handle the invalid input
The same applies for floats, of course.
Now, one great benefit of this is that the interpreter already knows
what makes a proper int (or float), and *you don't have to care*. Let
the interpreter deal with it, and only if it fails do you have to deal
with the invalid string.
By the way: absolute *none* of the turtle graphics code is the least bit
relevant to your question, and we don't need to see it all. That's a bit
like going to the supermarket to return a can of beans that you bought
because they had gone off:
"Hi, I bought this can of beans yesterday, but when I got it home and
opened it, they were all mouldy and green inside. Here's my receipt,
and the can, and here's the can opener I used to open them, and the bowl
I was going to put the beans into, and the microwave oven I would have
used to heat them up, and the spoon for stirring them, and the toast I had
made to put the beans on, and the salt and pepper shakers I use."
:-)
--
Steve
More information about the Tutor
mailing list