Rolling Your Own Expressions
Some standards organizations have published descriptions of exactly what makes up a legal value. Enterprising developers have transformed many of those descriptions into fairly portable regular expressions. Consider the following regular expression definition of a floating-point number:
^[+-]?[0-9]+[\.]?[0-9]*$
It’s not a perfect definition, but for many purposes, it’s a pretty good one—and a flexible one to boot. It accepts a pretty good range of floating-point numbers with optional signs at the start. Admittedly, as presented, it won’t accept –.75, but it will also not accept –., which I think offers a fair compromise because –0.75 isn’t too hard for the user to guess. Alternatively, you could use a set of regular ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access