Is regular expression a two-digit range (23-79)?

I read the regex questions on this site, but my problem seems to be a little different. I need to match a 2-digit number, for example from 23 to 75. I do this on an HP-UX Unix system. I found examples 3 - 44, but either any number of digits that are not fixed in length, which is a bit surprising, but maybe I do not understand the variable-length answer.

+6
source share
5 answers

Since you are not indicating whether this is in addition to any other characters (or in the middle of a larger line), I have included the logic here to indicate what you need to match the numeric part of the line, This should lead you there. We create a range for the second number we are looking for, only for those characters. Then we compare it with other ranges like or :

 (2[3456789]|[3456][0-9]|7[012345]) 

As noted, you can also do this, since subbands are also accepted (depends on the REGEX implementation in the application you use):

 (2[3-9]|[3-6][0-9]|7[0-5]) 

Based on the name, you will change the last 5 to 9 to go from 75-79:

 (2[3-9]|[3-6][0-9]|7[0-9]) 

If you are trying to match these numbers as a string (from start to finish), you should use the ^ and $ modifiers to indicate the start and end of the string.

There is an excellent technical link to Regex ranges here:

http://www.regular-expressions.info/numericranges.html

If you use something like grep and try to match strings containing a number with different content, you can do something like this for ranges through 79:

 grep "[^0-9]?(2[3-9]|[3-6][0-9]|7[0-9])[^0-9]?" folder 
+7
source

This tool is exactly what you need: Regex_For_Range

29 to 79: \b(2[3-9]|[3-7][0-9])\b

29 to 75: \b(29|[3-6][0-9]|7[0-5])\b

And just for fun, from 192 to 1742: \b(19[2-9]|[2-9][0-9]{2}|1[0-6][0-9]{2}|17[0-3][0-9]|174[0-2])\b :)

+3
source

If I need a 2-digit range 0-63

 /^[0-9]|[0-5][0-9]|6[0-3]$/ 
  • [0-9] allows you to use one digit from 0 to 9
  • [0-5] [0-9] will allow from 00 to 59
  • 6 [0-3] will allow 60 to 63

This way you can accept regex for any range with two digits

+1
source

You have two classes of numbers that you want to match:

  • number 2 followed by one of the numbers 3 to 9
  • one of the numbers 3 to 7, followed by any number

Edit: Good thing the heading range is (23-79). Within your question (23-75) you have three:

  • number 2 followed by one of the numbers 3 to 9
  • one of the numbers between 3 and 6, followed by any number
  • number 7 followed by one of the numbers 0 to 5
0
source

This should do it:

 /^([2][3-9]|[3-6][0-9]|[7][0-5])$/ 

^ and $ will make it strict that it matches only two numbers, so if you have ie 234 , it will not work.

-1
source

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


All Articles