Understanding Regular Expression for a Number Range

I am trying to create some regular expressions to test some textbox controls. I did some research and testing, but can't get it to work. Examples of what I'm trying to create regular expressions are as follows:

  • Range 0-45, 0 decimal places
  • Range 0-20, 2 decimal places
  • Range 16-65, 0 decimal places
  • Range 0-99, 2 decimal places
  • Range 0-1500000, 0 decimal places
  • Range 0-200, 1 decimal place.

For 1 and 5, respectively, I used

([0-9]|[0-9]\d|45)$  
([0-9]|[0-9]\d|1500000)$  

The first problem I came across is the age range 16-65 (inclusive), where I don't want decimals. After the message here ( Regular expression to allow numbers from -90.0 to +90.0 ) I thought I could use the logic and make it judge, but I can't!

The expression I received was:

(\d|([1-6][6-4]))|65  

Can someone tell me where I misunderstand this! And any help with the other examples above would be greatly appreciated.

+3
source share
2 answers

Sorry to say that, but none of your regular expressions will work. Remember that regular expressions are designed to match text data. Although you can use them to match numbers, it is not really a tool of choice.

, .

1 :

  • 1 3,
  • 4, 0 5.

:

^(?:\d|[1-3]\d|4[0-5])$

^ $ ; (?:...) "" .

3:

  • 1, 6-9
  • 2-5,
  • 6, 0-5

:

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

5:

  • 1-5
  • 1, 0-4,
  • 150000.

:

^(?:\d{1,5}|1[0-4]\d{4}|150000)$

.

:

  • \.\d{2}
  • \.\d{1,3} 1 - 3
  • (?:\.\d{1,2}) 0 2 ( , ).
+7

16 - 65 1 plus 6-9 OR 2-5 plus 0-9 OR 6 plus 0-5.

, 1[6-9]|[2-5][0-9]|6[0-5]

+2

Source: https://habr.com/ru/post/1787967/


All Articles