Entry
How do you enter a numeric range (e.g. 0-255) in an RE when matching?
Mar 27th, 2003 09:32
Michael Chermside, Brandon Ade,
You've heard the old saying "When all you have is a hammer, the world
looks like a nail." Well, in this vein, regular expressions are kind of
like a swiss army knives. They seem like they can be used for just
about anything, but they're hardly ever the best tool for the job.
It is unlikely that you actually want to use a RE for anything which
involves recognizing numbers *and caring what the values are*. So
consider carefully what you're doing and whether you're better off
using a parser, or some other such tool.
But if you're convinced you want a regular expression (and I'm guessing
you're thinking about the classic problem of a RE to describe an IP
address here), then here's an answer:
"[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]"
Ugly, but it works.
-- Michael Chermside