Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression - Two Digit Range (23-79)?

Tags:

regex

I have been reading the regex questions on this site but my issue seems to be a bit different. I need to match a 2 digit number, such as 23 through 75. I am doing this on an HP-UX Unix system. I found examples of 3 - 44 but or any digit number, nothing that is fixed in length, which is a bit surprising, but perhaps I am not understand the variable length example answer.

like image 743
user2414817 Avatar asked May 23 '13 18:05

user2414817


People also ask

How do you do a range in regex?

Example: Regex Number Range 1-20 Range 1-20 has both single digit numbers (1-9) and two digit numbers (10-20). For double digit numbers we have to split the group in two 10-19 (or in regex: "1[0-9]") and 20. Then we can join all these with an alternation operator to get "([1-9]|1[0-9]|20)".

What does \+ mean in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" .

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1. 1* means any number of ones.

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .


2 Answers

If I want 2 digit range 0-63

/^[0-9]|[0-5][0-9]|6[0-3]$/
  1. [0-9] will allow single digit from 0 to 9
  2. [0-5][0-9] will allow from 00 to 59
  3. 6[0-3] will allow from 60 till 63

This way you can take Regular Expression for any Two Digit Range

like image 149
Kapilrc Avatar answered Sep 21 '22 00:09

Kapilrc


Since you're not indicating whether this is in addition to any other characters (or in the middle of a larger string), I've included the logic here to indicate what you would need to match the number portion of a string. This should get you there. We're creating a range for the second numbers we're looking for only allowing those characters. Then we're comparing it to the other ranges as an or:

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

As oded noted you can do this as well since sub ranges are also accepted (depends on the implementation of REGEX in the application you're using):

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

Based on the title you would change the last 5 to a 9 to go from 75-79:

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

If you are trying to match these numbers specifically as a string (from start to end) then you would use the modifiers ^ and $ to indicate the beginning and end of the string.

There is an excellent technical reference of Regex ranges here:

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

If you're using something like grep and trying to match lines that contain the number with other content then you might do something like this for ranges thru 79:

grep "[^0-9]?(2[3-9]|[3-6][0-9]|7[0-9])[^0-9]?" folder
like image 34
AbsoluteƵERØ Avatar answered Sep 19 '22 00:09

AbsoluteƵERØ