1

I want regular expression to accept 1 - 9 numeric digits.

May not be in the ranges:

-0090000000 - 009499999,0900000000 - 094000000

I have tried the following, but it is not working:

^([^0900000000-009499999]|[^0900000000-094000000]|\d{9}|\d{1}|\d{2}|\d{3}|\d{4}|\d{5}|\d{6}|\d{7}|\d{8})$
Wes Foster
  • 8,770
  • 5
  • 42
  • 62
  • You will have to read a lot about regular expressions. See http://regex.learncodethehardway.org/book/ or http://regex.info/book.html – HamZa Sep 19 '15 at 18:38
  • Could you use a regex to check for 1-9 numeric digits, then parse it as an integer and check the range with `If...Then`? – Andrew Morton Sep 19 '15 at 18:50
  • are these ranges inclusive? [0900000000-0940000000] or partially exclusive? [0900000000-0940000000) – d0nut Sep 19 '15 at 21:30
  • These ranges are weird too. Why is the right hand number smaller? – d0nut Sep 19 '15 at 21:33
  • There's some inconsistency... `009499999` has only nine digits while the other numbers have ten. Do leading zeros are significative or important? if so, where do the ranges switch from nine digits to ten? Please update. I'm waiting to solve for this inconsistency to publish my response, with a valid regexp to check for both integer ranges. If erroneos, does it mean `0009499999` or `0094999999` ??? If correct, in which range do you switch from `9` digit numbers to `10`??? – Luis Colorado Sep 20 '15 at 09:49
  • As I understand, the first range goes from negative extreme (`-0090000000`) to positive number (`009499999`) I think this is the reason for it being lesser than the left extremum (in absolute value) Please confirm this also. – Luis Colorado Sep 20 '15 at 09:57
  • 1
    possible duplicate of [Why doesn't \[01-12\] range work as expected?](http://stackoverflow.com/questions/3148240/why-doesnt-01-12-range-work-as-expected) – Andrew Morton Sep 21 '15 at 10:03

1 Answers1

1

I agree with Andrew, the use of regex is for detecting regular languages not for calculations and decide on the result. I would convert the string to an integer and then compare the boundaries.

Any language with "if" statements and one stack/heap is much more powerful than regex automatons, that is why regexs languages are a substet of turing machines ones.

I think regex are overused and misunderstood. Most of the time they are not needed and cause very confusing code. In fact, regex languages may vary depending on the language they belong to As analogy, C# language is very different from C++ but both are turing complete (Both have jump statements (if, while, etc) and can allocate, read and write memory. So both can solve the same kind of problems (category) but the syntax and concepts behind them are different.

The same thing happens with regex notations. Perl and C# may use different notation/languages for matching expressions for a given text. Some extensions have been added to those notations to support more complex languages. But if you use those extension then the new regex is not regex anymore. In that case I would use control statements of the host language (if, while, variables(. It is the natural way of doing computations.

Martin A
  • 132
  • 10
  • Deciding if a string represents a number in a range can be done by a finite automaton without any calculation. It's by far the most effective way to do it, as only requires an array lookup and an assignment per character, and only one decision is made at the end. I'm full of implementing finite automata to check things like calculating checksums, checking for multiples of some strange number and the like. Why not represent these finite automata with their equivalent regexp? Remember that you write the code only once to be executed many times. – Luis Colorado Sep 20 '15 at 10:06
  • Agree, but IMO it is not natural and hard to understand. In addition, when you use a regex you may be adding complexity since you are dealing with two notations that work in different ways. C# and C++ are standard languages formally described by documents approved by experts in the domain that are part of commites (Like ISO standards) . One thing that makes a regex a bit better is that you can induce things. For instance, you can probe if two automatons are equivalent , what it does and what it does not, and so on. Still, I believe they are overused in many cases. – Martin A Sep 22 '15 at 02:53