user2414817
user2414817

Reputation: 153

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

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.

Upvotes: 15

Views: 22141

Answers (6)

user1763510
user1763510

Reputation: 1270

Just to add to this, here is a solution for generating the string from the accepted answer in javascript. You can click "Run Code Snippet" to enter your own bounds and get your own string.

function regexRangeString(lower,upper){
  let current=lower;
  let nextRange=function(){
    let currentString=String(current);
    let len=currentString.length;
    let string="";
    let newUpper;
    for(let digit=0;digit<len;digit++){
      let index=len-digit-1;
      let lower=Number(currentString[index]);
      let thisString="";
      for(let u=9;u>=lower;u--){
        let us=currentString.substring(0,index)+u+currentString.substring(index+1,len);
        if(Number(us)<=upper){
          if(lower==u){
            thisString=lower;
          }
          else{
            thisString=`[${lower}-${u}]`;
          }
          currentString=currentString.substring(0,index)+u+currentString.substring(index+1,len);
          break;
        }
      }
      if(thisString!="[0-9]"){
        string=currentString.substring(0,index)+thisString+string;
        break;
      }
      else{
        string=thisString+string
      }
    } 
    current=Number(currentString)+1;
    return string
  }

  let string=""
  while(current<upper){
    string+="|"+nextRange(current);
  }
  string="("+string.slice(1)+")";
  return string
}


let lower=prompt("Enter Lower Bound")
let upper=prompt("Enter Upper Bound")
alert(regexRangeString(lower,upper))

For example:

regexRangeString(72,189)

generates the following output string:

(7[2-9]|[8-9][0-9]|1[0-8][0-9])

Upvotes: 0

sp00m
sp00m

Reputation: 48837

This tool is exactly what you need: Regex_For_Range

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

From 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 :)

Upvotes: 4

AbsoluteƵER&#216;
AbsoluteƵER&#216;

Reputation: 7880

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

Upvotes: 16

Kapilrc
Kapilrc

Reputation: 1538

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

Upvotes: 2

Desolator
Desolator

Reputation: 22759

This should do it:

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

^ and $ will make it strict that it will match only 2 numbers, so in case that you have i.e 234 it won't work.

Upvotes: -2

John
John

Reputation: 16007

You have two classes of numbers you want to match:

  • the digit 2, followed by one of the digits between 3 and 9
  • one of the digits between 3 and 7, followed by any digit

Edit: Well, that's the title's range (23-79). Within your question (23-75), you have three:

  • the digit 2, followed by one of the digits between 3 and 9
  • one of the digits between 3 and 6, followed by any digit
  • the digit 7, followed by one of the digits between 0 and 5

Upvotes: 0

Related Questions