Page 1 of 1
Select numbers greater/less than?
Posted: Mon Oct 08, 2012 5:42 pm
by uwskier25
This has to be simple, yet I can't find an answer anywhere. How do I find numbers greater than a given value? I can't simply search for >200, for example.
Thanks!
Posted: Mon Oct 08, 2012 8:57 pm
by ben_josephs
It isn't simple. Regular expressions can't count: all they know about is uninterpreted characters. But you can do this with a certain amount of effort. For the specific example you give, try
\<(20[1-9]|2[1-9][0-9]|[3-9][0-9]{2}|[0-9]{4,})\>
This assumes you are using "Posix" regular expression syntax:
Configure | Preferences | Editor
[X] Use POSIX regular expression syntax
Posted: Mon Oct 08, 2012 9:02 pm
by uwskier25
Ack! I can't even make sense of that. :) Seems like it should be a simple request.
Thanks, though. Your example is helpful.
Posted: Tue Oct 09, 2012 12:24 am
by ak47wong
To expand on ben_joseph's answer, it may help to understand how the expression is put together:
20[1-9] matches 201 to 209 (20 followed by 1-9).
2[1-9][0-9] matches 210 to 299 (2 followed by 1-9, followed by 0-9).
[3-9][0-9]{2} matches 300 to 999 (3-9 followed by two occurrences of 0-9).
[0-9]{4,} matches 1000 and higher (4 or more occurrences of 0-9).
| means "or", ( and ) group the expression together, and \< and \> anchor the expression to the start and end of a word.
You can extrapolate this to construct an expression for other numbers.
Posted: Wed Oct 10, 2012 8:13 am
by kengrubb
[0-9]{4,} also matches 0000
[1-9][0-9]{3,} matches 1000 or more
Posted: Wed Oct 10, 2012 8:27 am
by ben_josephs
Quite right. But leading zeros usually don't have to be dealt with. If they do, use this instead of my previous suggestion:
\<0*(20[1-9]|2[1-9][0-9]|[3-9][0-9]{2}|[1-9][0-9]{3,})\>
Edit: corrected typo.
Posted: Thu Oct 11, 2012 4:13 am
by kengrubb
I'm a Cobolasaurus. I dream in leading zeroes.
Posted: Thu Oct 11, 2012 7:02 am
by ben_josephs
Didn't they die out...?
Posted: Thu Oct 11, 2012 7:42 am
by kengrubb
Not yet. Most are still admiring the bright light in the sky.
Solution updated for Numbers falling at the end of a line!
Posted: Wed Jan 15, 2014 3:49 am
by 1ND1G0
ben_josephs' solutions work fine EXCEPT when the numbers fall at the end of a line.
\< and \> anchor the expression to the start and end of a word but this FAILS at the end of a line.
Hence we need the following:
\<0*(20[1-9]|2[1-9][0-9]|[3-9][0-9]{2}|[1-9][0-9]{3,})($|\>)
to accommodate this possibility!
Posted: Wed Jan 15, 2014 7:40 am
by ben_josephs
\< and \> and the regex I suggested that contains them work as expected on my machine, including in the case where the number is at the end of a line, in both versions 6.1 and 7.1.0 of TextPad.
And \b works as expected in version 7.1.0.
Posted: Wed Jan 15, 2014 8:37 am
by 1ND1G0
You are right.
My mistake was that when I copied your RegEx I picked up a trailing 'space' character which caused the malfunction.
It seems to be a quirk of the Forum that it allows you to pick up a 'space', that shouldn't actually be there, when you copy a line!
Thanks for your quick response!