0

What input string will the following regex expression match:

[1-4]{0-5}

I know the first part: [1-4] specifies the numerals allowed in the input, and the latter the length? But it does not match any of the expected inputs, such as 1112, 123..

faizanjehangir
  • 2,401
  • 3
  • 37
  • 77
  • 3
    The way you've changed the question makes the existing answers look very odd. If that pattern really isn't matching "1112" please provide a [mcve] so we can try to reproduce the issue. – Jon Skeet Jun 03 '18 at 07:28

2 Answers2

3

Try the following regex,

It should be :

[1-4]{0,5}

This will match:

empty string  = because {0,5} means zero to 5 length of character
1
11
111
1111
11111
2
22
2222

1
12
1234
12344
and it goes on and on but nothing beyond the digit 4, and no length beyond 5

Regex101Demo

Rizwan M.Tuman
  • 9,424
  • 2
  • 24
  • 40
3

The latter is not the length. If it were the length, it would be {0,5} with a comma instead of a -.

Without the comma, it will just match {0-5} literally, so this matches:

1{0-5}

I think you want

[1-4]{0,5}
Sweeper
  • 145,870
  • 17
  • 129
  • 225