How to represent greater than or equal 3600 in regex
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.
Related videos on Youtube
user9371654
Updated on September 18, 2022Comments
-
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 about 5 yearsDo you want to exclude a number like
3600.0
? -
user9371654 about 5 yearsNo 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 about 5 years@Jeff Schaller No upper limit. Can you propose a fix?
-
Angel Todorov about 5 yearsRegular expressions are really the wrong hammer for this job.
-
-
user9371654 about 5 yearsBut you limit the number's length. How about this number: 31536000 or this: 31536000000?? Can you please explain your regex a bit?
-
user9371654 about 5 yearsIt does not capture this: 36000 although it is greater than 3600.
-
user9371654 about 5 yearsI have tested it in
36000
. It does not match the last zero. Only matches3600
part. May be te last part in you regex should be[0-9]{2,}
instead of[0-9]{2}
?? -
Angel Todorov about 5 yearsYou'll want to add anchors:
\<(...)\>
-
Philippos about 5 yearsSorry! 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 about 5 years
-P
does left-to-right, but-E
does longest. -
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 on03600
.