1

Is there a thing like regular expressions for numbers? I mean a handy notation to specify constraints for numbers. Like say that a number to match must be an odd natural number between -10 and 1000 but not between 3 and 14 and must have a digit 8 in it.

Ivan
  • 63,011
  • 101
  • 250
  • 382
  • 5
    Most of the constaints you mention are exactly the kind of thing that regular expressions (and finite state machines in general) are the wrong tool for, because they involve arithmetic. What's wrong with good old fashioned operators? `n % 2 == 0`, `-10 <= n && n <= 1000 && !(3 <= n && n <= 14)`. Only the "contains a digit" part is actually a string operation. –  Apr 28 '14 at 13:32
  • why regex should be the tool to solve it? could it be a X->Y problem? – Kent Apr 28 '14 at 13:33
  • possible duplicate of [Reference - What does this regex mean?](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) under **Common Validation Tasks** – HamZa Apr 28 '14 at 13:33

1 Answers1

1

Regular expressions are used to express or define regular languages, i.e. the type 3 languages on the Chomsky hierarchy. These languages are those that can be accepted by finite automata, both deterministic and non-deterministic.

The example you have in your question appears to describe languages that cannot be defined by regular expressions, thus cannot be a regular language. Actually it would require an unrestricted grammar, i.e. a Turing-complete language to define your example language.

Luckily, we have plenty of those in the form of programming languages. So the regular expression for numbers that you outline in your question is any programming language!

As for a handy notation, your question seems to call for a series of filters to be applied. Many languages make this easy to express through the use of lambda expressions and higher order functions.

ggovan
  • 1,907
  • 18
  • 21