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

前端 未结 5 1233
天涯浪人
天涯浪人 2021-02-15 18:31

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 H

5条回答
  •  伪装坚强ぢ
    2021-02-15 18:45

    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
    

提交回复
热议问题