Select numbers greater/less than?

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
uwskier25
Posts: 2
Joined: Mon Oct 08, 2012 5:40 pm

Select numbers greater/less than?

Post 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!
ben_josephs
Posts: 2459
Joined: Sun Mar 02, 2003 9:22 pm

Post 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
uwskier25
Posts: 2
Joined: Mon Oct 08, 2012 5:40 pm

Post 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.
ak47wong
Posts: 703
Joined: Tue Aug 12, 2003 9:37 am
Location: Sydney, Australia

Post 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.
User avatar
kengrubb
Posts: 324
Joined: Thu Dec 11, 2003 5:23 pm
Location: Olympia, WA, USA

Post by kengrubb »

[0-9]{4,} also matches 0000

[1-9][0-9]{3,} matches 1000 or more
(2[Bb]|[^2].|.[^Bb])

That is the question.
ben_josephs
Posts: 2459
Joined: Sun Mar 02, 2003 9:22 pm

Post 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.
Last edited by ben_josephs on Thu Oct 11, 2012 7:02 am, edited 1 time in total.
User avatar
kengrubb
Posts: 324
Joined: Thu Dec 11, 2003 5:23 pm
Location: Olympia, WA, USA

Post by kengrubb »

I'm a Cobolasaurus. I dream in leading zeroes.
(2[Bb]|[^2].|.[^Bb])

That is the question.
ben_josephs
Posts: 2459
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Didn't they die out...?
User avatar
kengrubb
Posts: 324
Joined: Thu Dec 11, 2003 5:23 pm
Location: Olympia, WA, USA

Post by kengrubb »

Not yet. Most are still admiring the bright light in the sky.
(2[Bb]|[^2].|.[^Bb])

That is the question.
1ND1G0
Posts: 7
Joined: Fri Dec 05, 2008 6:41 am

Solution updated for Numbers falling at the end of a line!

Post 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!
ben_josephs
Posts: 2459
Joined: Sun Mar 02, 2003 9:22 pm

Post 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.
1ND1G0
Posts: 7
Joined: Fri Dec 05, 2008 6:41 am

Post 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!
Post Reply