How to represent greater than or equal 3600 in regex

5,268

Solution 1

The extended regular expression ([1-9][0-9]{2,}|[4-9][0-9]|3[6-9])[0-9]{2} should do the job if you can be sure there are no negative numbers, floats, thousand limiters and so on around

The expression has three paths, all of which have in common the last part [0-9]{2}, which means two digits.

  • First path is a 1 to 9 with at least two more digits ([0-9]{2,}) and those common twi digits, so it's all numbers with five or more digits: 10000 and above
  • Second path is a 4 to 9 with three more digits: 4000 to 9999
  • Third path is a 3 and something from 6 to 9 and those two digits. This matches everything between 3600 and 3999

Solution 2

Regex is not good comparing numbers !

Better use some scripting language. In your case awk would do a good job:

awk -F '[^0-9]*' '{for(i=1;i<=NF;i++){ if (int($i)>3600) { print; next; } }}' test.txt

Dependend on your input you should adapt this a bit.
My short example would e.g. not work correctly with negative numbers.

Share:
5,268

Related videos on Youtube

user9371654
Author by

user9371654

Updated on September 18, 2022

Comments

  • user9371654
    user9371654 almost 2 years

    I wrote this regex to match numbers greater than or equal 3600. This is my attempt. However, I am not sure if it is complete:

    grep -P '36[0-9]+[0-9]+[0-9]*' test.txt
    

    I mean positive decimal integer numbers only (I do not need to consider floating, negative numbers, octal, hexadecimal, roman numerals...).

    • Jeff Schaller
      Jeff Schaller about 5 years
      Do you want to exclude a number like 3600.0?
    • user9371654
      user9371654 about 5 years
      No floats, negatives or any kind of numbers mentioned by @Stéphane Chazelas. Just simple integers 2600, 3601, 3602, etc. They appear anywhere on the line.
    • user9371654
      user9371654 about 5 years
      @Jeff Schaller No upper limit. Can you propose a fix?
    • Angel Todorov
      Angel Todorov about 5 years
      Regular expressions are really the wrong hammer for this job.
  • user9371654
    user9371654 about 5 years
    But you limit the number's length. How about this number: 31536000 or this: 31536000000?? Can you please explain your regex a bit?
  • user9371654
    user9371654 about 5 years
    It does not capture this: 36000 although it is greater than 3600.
  • user9371654
    user9371654 about 5 years
    I have tested it in 36000. It does not match the last zero. Only matches 3600 part. May be te last part in you regex should be [0-9]{2,} instead of [0-9]{2}??
  • Angel Todorov
    Angel Todorov about 5 years
    You'll want to add anchors: \<(...)\>
  • Philippos
    Philippos about 5 years
    Sorry! The mistake was the order of the paths: When the first path generates a match (3600), the other paths are not checked anymore, so the longest match needs to be in the first path. I reordered the paths in my answer
  • Stéphane Chazelas
    Stéphane Chazelas about 5 years
    -P does left-to-right, but -E does longest.
  • Stéphane Chazelas
    Stéphane Chazelas about 5 years
    @glennjackman, anchors won't help in this case (note that it's \b with -P (or -w)) it would also mean it wouldn't match on 03600.