5

I recently just picked up on regex and I am trying to figure out how to match the pattern of any numbers greater than 1. so far I came up with

[2-9][0-9]*

But it only works with the leftmost digit not being 1. For example, 234 works but 124 doesn't.

So what am I trying to achieve is that a single digit of 1 shouldn't be matched and any integer greater than it should.

PegasusFantasy
  • 93
  • 1
  • 1
  • 7

3 Answers3

11

You should be using alteration to define two categories of numbers.

  1. Less than 10.
  2. Greater than or equal to 10.

Regex: ^(?:[2-9]|\d\d\d*)$

Explanation:

[2-9] is for numbers less than 10.

\d\d\d* is for numbers greater than or equal to 10.

Regex101 Demo

Alternate solution considering preceding 0

Regex: ^0*(?:[2-9]|[1-9]\d\d*)$

Regex101 Demo

Rahul
  • 2,658
  • 12
  • 28
  • hm, that seems pretty advanced. why are there two categories and why the number 100??? – PegasusFantasy Apr 23 '17 at 17:18
  • It doesn't match 10 and matches 001. – Casimir et Hippolyte Apr 23 '17 at 17:18
  • @CasimiretHippolyte: Oh blimey ! need to format it a little bit. I guess `[2-9]\d*|\d\d\d\d*|10` will solve the problem. – Rahul Apr 23 '17 at 17:19
  • @CasimiretHippolyte I don't get how 1 wouldn't work in this case, wouldn't `\d*` matches with any digit? – PegasusFantasy Apr 23 '17 at 17:23
  • @PegasusFantasy: I have updated my answer. Two categories because numbers less than 10 have only one digit, we can simply match it with `[2-9]`. Numbers larger than 9 will have more than 2 digits in which case there should atleast be 2 digits, 3rd and beyond that are optional. – Rahul Apr 23 '17 at 17:26
  • How about replacing the second part to `[1-9]\d+` for disallowing numbers that start with zero. But it's not clear from question if such as `001` is allowed or not. However, your current regex would allow `00` which is probably not wanted. – bobble bubble Apr 23 '17 at 17:28
  • 7
    this matches `000000`, ***Stop trying to do math operations with a regex***, it's really sad to see this. ***You cannot assert if a number is greater, equal or smaller than other with a regex*** – Pedro Lobito Apr 23 '17 at 17:29
  • @bobblebubble: Since OP didn't mentioned about combination of digits like `00` or `0000`. – Rahul Apr 23 '17 at 17:30
  • @PedroLobito: I know right. But incase it's not a calculation and just pattern matching ? Not clear in question. – Rahul Apr 23 '17 at 17:34
  • Depending on the regex flavour (not specified in the question), `\d` may match more than you expect. And it should be noted that it doesn't accept other representations of integers that are usually accepted, such as `+1000` or `1,000`. The OP should have specified the regex flavour and precisely what should and shouldn't be accepted, and personally, I think at least until that time, the question should not have received an answer yet, it's unavoidably too much based on guesswork. –  Apr 23 '17 at 18:09
  • Would think most applications of this regex would want to disallow numbers with trailing zeros (e.g. `002`). Would suggest revamping this answer to give`^(?:[2-9]|[1-9]\d+)$` as the primary solution. – Steve Chambers Feb 04 '22 at 12:03
5

This should do the trick. [0]*([2-9]+|[1-9][0-9][0-9]*)

pradosh nair
  • 913
  • 1
  • 16
  • 28
3

Use this

^[2-9]|[1-9]\d+$

See example here