-4

I need to validate string input with regex, rules are:

  • String should not be number less than 2 and not bigger than 9999 (2-9999)
  • String should not have zeros before number (ex: no 0002, 0022, 0222)

I really need to accomplish this by regex so any other solution is not acceptable.

GoR
  • 121
  • 2
  • 10
  • 3
    (1) Why regex? This is not the easiest way to do this. (2) Have you tried anything? – elixenide Apr 15 '16 at 02:23
  • 5
    You seem to have mistaken us for a regex writing service. What effort have you made to do this yourself? If you *really need to accomplish this by regex*, I'd think you could at least make an effort to do so. – Ken White Apr 15 '16 at 02:34
  • @EdCottrell Your first question seems odd *(Why regex)* .. How can he do that without regex? Have you any better alternative? – Shafizadeh Apr 15 '16 at 13:24
  • @Shafizadeh As a rule, it's better to convert or parse a string into numeric data and then deal with the numbers directly. Trying to find numbers in a particular range in textual data is much harder than simply extracting the numbers and comparing them to the boundary numbers. There are times it's unavoidable, but the OP didn't specify his constraints. Without more info, this sounds like an instance of [the XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – elixenide Apr 15 '16 at 13:59

2 Answers2

2

Try this:

/^[2-9]|[1-9][0-9]{1,3}$/

To implement your first condition:

  • String should not be number less than 2 and not bigger than 9999 (2-9999)

There is two cases:

  1. Single digits : [2-9] This is a single character in the range between 2 and 9.
  2. Multiple digits: [1-9][0-9]{1,3} This is a two-three-four-digit number which all digits are in the range 1 and 9.

Note1: {1,3} limits second character class to just accept one or two or three digits.

Note2: ^ means start of string and $ means end of string.

By the way, your second condition isn't defined in pattern above at all. (I mean it doesn't match any number which stars with 0, So all fine.)

Shafizadeh
  • 9,960
  • 12
  • 52
  • 89
  • http://meta.stackexchange.com/q/196187/172661 – Ken White Apr 15 '16 at 02:46
  • Thanks...it is working perfectly :) – GoR Apr 15 '16 at 02:47
  • @GoR You are welcome. – Shafizadeh Apr 15 '16 at 02:53
  • @KenWhite Yes you are right. The most of answers need at least a short explanation. But in this case, the explanation of my solution would be those two conditions in the question above. – Shafizadeh Apr 15 '16 at 02:55
  • No, for the reasons in the answers to that post, and most specifically because you shouldn't have to suggest that the poster *try* anything. You should be posting an answer, not something for the poster to *try*., and because you should be *educating future readers*. You've also done an excellent job of teaching this poster (and others in the future) that *plz give me teh codez* questions are acceptable here by removing all incentive to make an effort themselves first, and encouraging future people answering to not bother to explain their answers. Kudos. – Ken White Apr 15 '16 at 03:00
  • @KenWhite Fair enough .. I think I have to stop writing answer anymore. because I don't know English very well and I just can write code for people ..! – Shafizadeh Apr 15 '16 at 03:07
  • Your English is good enough to understand the question, and good enough to have this conversation with me. It's good enough to explain your answers. If you're not willing to do so, then you probably should stop answering questions; this site exists for the entire purpose of collecting knowledge, and if you can't explain your code you're not sharing knowledge, but just writing code. – Ken White Apr 15 '16 at 03:12
  • @KenWhite I added a few explanations to my answer. But I'm pretty sure they aren't completely correct. *(English language perspective)* – Shafizadeh Apr 15 '16 at 03:29
0

Try this

^(?!0|1$)\d{1,4}$

Regex demo

Explanation:
^\d{1,4}$: matches 0-9999
(?!0)...: not have zeros before number (ex: no 0002, 0022, 0222)
(?!1$)...: not be number less than 2 (==1)
(?!…): Negative lookahead sample
\d: One digit from 0 to 9 sample
^: Start of string or start of line depending on multiline mode
$: End of string or end of line depending on multiline mode

Tim007
  • 2,557
  • 1
  • 11
  • 20