Gossamer Forum
Home : General : Perl Programming :

regex for 1-3 figures, but not 0

Quote Reply
regex for 1-3 figures, but not 0
Smile
Hi guys,

I am looking for a regex for 1-3 figures, such as 145 or 44 or 54, but the value cannot be 0.

Anyone an idea?

Stuart
Quote Reply
Re: [vacationrentals] regex for 1-3 figures, but not 0 In reply to
Hi Stuart,

Can you try this?

(thenumber) = $str=~ m,([1-9]\d+),;

Cheers,

Cheers,

Dat

Programming and creating plugins and templates
Blog
Quote Reply
Re: [tandat] regex for 1-3 figures, but not 0 In reply to
Hi Dat,

thanks for responding. I will try it, but it looks it bit odd to me. The regex I am using in the Links SQL database property field is a bit different, it looks like ^[0-9]{1,8}$

But that doens't mean anything. I am just a user, not an expert.

Stuart
Quote Reply
Re: [vacationrentals] regex for 1-3 figures, but not 0 In reply to
Its a little difficult, as I'm guessing you need to use 0 in the string? i.e;

405 would be ok
045 would NOT be ok
123 would be ok
450 would be ok

Am I correct?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] regex for 1-3 figures, but not 0 In reply to
Hi Andy,

good to hear from you. No, actually, ONLY 0 is not allowed.

stuart
Quote Reply
Re: [vacationrentals] regex for 1-3 figures, but not 0 In reply to
In that case, how about this;

^123456789+$

?

Regex isn't my strongest point (normally get there by trial and error).

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] regex for 1-3 figures, but not 0 In reply to
Tried that, but then I do not have 40 in there which should be allowed.

Cheers
Quote Reply
Re: [vacationrentals] regex for 1-3 figures, but not 0 In reply to
Not sure what you mean Unsure

Can you give me say 5 examples of what would be alowed, and 5 that would not be allowed?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] regex for 1-3 figures, but not 0 In reply to
Sure.

20
32
54
24
44
49

All allowed.

0
01
02
03
04

Not allowed
Quote Reply
Re: [vacationrentals] regex for 1-3 figures, but not 0 In reply to
In that case, this should work;

^[123456789]{,1}[0-9]{1,5}$

Does that do the job?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!

Last edited by:

Andy: Jun 30, 2004, 3:40 AM
Quote Reply
Re: [Andy] regex for 1-3 figures, but not 0 In reply to
Sorry, it doesn't. It cannot contain the value 40 this way.
Quote Reply
Re: [vacationrentals] regex for 1-3 figures, but not 0 In reply to
How does the modified version work? (above post).

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] regex for 1-3 figures, but not 0 In reply to
Thanks for that, but it didn't work. For example it did not let enter 40 as a number.
Quote Reply
Re: [vacationrentals] regex for 1-3 figures, but not 0 In reply to
Hi Stuart

please try the following regex:

Code:

^(?:(?!0)\d\d\d)$


Hope it helps...

Cheers,
Smileliver
Quote Reply
Re: [olivers] regex for 1-3 figures, but not 0 In reply to
Thnaks Oliver,

didn't do it, unfortunately.

Stuart
Quote Reply
Re: [vacationrentals] regex for 1-3 figures, but not 0 In reply to
Well, I've tested it and it works on my system. Please let me know how/where you've implemented the code exactly and what kind of errors/problems you're experiencing now.

Thanks
Unsureliver
Quote Reply
Re: [olivers] regex for 1-3 figures, but not 0 In reply to
Hi oliver,

I appreciate you are taking the time. When I am trying to enter 40, the system tells me that I can't, which is a result based on the regex.

Stuart
Quote Reply
Re: [vacationrentals] regex for 1-3 figures, but not 0 In reply to
Hi Stuart,

oooops, you're right. I've tested it with three numbers like 400, 411, 004 etc. The sample I've posted requires three numbers and doesn't allow you to enter two or one of them.

Just modify the regex like this:

Code:

^(?:(?!0)\d{1,3})$


I hope this one works for you - good luck!

Cheers,
Blushliver
Quote Reply
Re: [ALL] regex for 1-3 figures, but not 0 In reply to
BTW, a good resource to test regex can be found here:

http://www.regexlib.com/RETester.aspx

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln
Quote Reply
Re: [dan] regex for 1-3 figures, but not 0 In reply to
Cool Dan, your link is very helpful. It's faster and more comfy to test regular expressions via webform than writing a special test script!

Thanks for your input
Coolliver
Quote Reply
Re: [olivers] regex for 1-3 figures, but not 0 In reply to
Hi Oliver!

Well done. Good Job. Thanks for that.
Quote Reply
Re: [olivers] regex for 1-3 figures, but not 0 In reply to
Howz about:

Code:

^[1-9]\d{,2}$


Smile
Quote Reply
Re: [BlueBottle] regex for 1-3 figures, but not 0 In reply to
Hi BlueBottle

we've got it working with our regex (as already mentioned). I've just checked your regex and it doesn't seem to do what vacationrentals wanted - have you been able to test it?

Oliver
Quote Reply
Re: [olivers] regex for 1-3 figures, but not 0 In reply to
Just for the sake of perl golf

^[1-9]\d?\d?$

-gordon

s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';
Quote Reply
Re: [GClemmons] regex for 1-3 figures, but not 0 In reply to
Hi Gordon

your input is very much appreciated.
^[1-9]\d?\d?$ works perfectly and I'd say you've got a hole in one Wink.
We all hope that you'll play a lot of perl golf in this forum.

Thanks and all the best,
Smileliver