Regular expression problem

B

bruce

Hi. Not exactly a regex pro, but usually I know enough to get by. Here
is my problem:

I wrote a regex to edit a decimal number. you're allowed 1,2, or 3
digits before the decimal point, and 0,1,2,or 3digits after the decimal
point. Of course, you can't enter a decimal point without at least a
digit after ("5." is invalid). So here is my regex

pattern=/^\d{1,3}(\.(?=\d))\d{0,3}$/

This works fine for every case except an integer. In other words, it
tests false for entering 5, or 567.
I don't see why it tests false for integers. I'm allowing 1-3 digits
before the decimal point, then a decimal point only if the next
character is a digit (the lookahead clause), and then 0-3 digits after
the decimal point.
I've gotten around this problem with other javascript code around
the regex, but I'd just like to know why this "clean" solution doesn't
work.
Thanks for your help. This is a great group, I've learned so much
from the very knowledgable people here (including much regex stuff!)
 
M

Martin Honnen

bruce said:
I wrote a regex to edit a decimal number. you're allowed 1,2, or 3
digits before the decimal point, and 0,1,2,or 3digits after the decimal
point. Of course, you can't enter a decimal point without at least a
digit after ("5." is invalid). So here is my regex

pattern=/^\d{1,3}(\.(?=\d))\d{0,3}$/

Can't you simply use e.g.
var pattern = /^\d{1,3}(\.\d{1,3})?$/;
That allows one to three digits followed by nothing or by a decimal
point followed by one to three digits.
Still in real life you usually don't want e.g.
000
which that regular expression would allow.
 
E

Evertjan.

bruce wrote on 08 jun 2006 in comp.lang.javascript:
Hi. Not exactly a regex pro, but usually I know enough to get by. Here
is my problem:

I wrote a regex to edit a decimal number. you're allowed 1,2, or 3

Edit? It seems you want to test a string?
digits before the decimal point, and 0,1,2,or 3digits after the decimal
point. Of course, you can't enter a decimal point without at least a
digit after ("5." is invalid). So here is my regex

pattern=/^\d{1,3}(\.(?=\d))\d{0,3}$/

This works fine for every case except an integer. In other words, it
tests false for entering 5, or 567.
I don't see why it tests false for integers. I'm allowing 1-3 digits
before the decimal point, then a decimal point only if the next
character is a digit (the lookahead clause), and then 0-3 digits after
the decimal point.
I've gotten around this problem with other javascript code around
the regex, but I'd just like to know why this "clean" solution doesn't
work.
Thanks for your help. This is a great group, I've learned so much
from the very knowledgable people here (including much regex stuff!)

The look-ahead is not supported on all browsers, as I heard whispering.
You probably would not want a leading 0, if >=10

<script type='text/jscript'>

function t(n){
return /^(([1-9]\d{0,2})||\d)(||(\.\d{1,3}))$/.test(n)
}

alert(t('.1')); // false
alert(t('1234')); // false
alert(t('123')); // true
alert(t('012')); // false
alert(t('123.')); // false
alert(t('2.77')); // true
alert(t('0.770')); // true
alert(t('0.7700')); // false

</script>
 
B

bruce

Evertjan. said:
bruce wrote on 08 jun 2006 in comp.lang.javascript:
Hi. Not exactly a regex pro, but usually I know enough to get by. Here
is my problem:

I wrote a regex to edit a decimal number. you're allowed 1,2, or 3

Edit? It seems you want to test a string?
digits before the decimal point, and 0,1,2,or 3digits after the decimal
point. Of course, you can't enter a decimal point without at least a
digit after ("5." is invalid). So here is my regex

pattern=/^\d{1,3}(\.(?=\d))\d{0,3}$/

This works fine for every case except an integer. In other words, it
tests false for entering 5, or 567.
I don't see why it tests false for integers. I'm allowing 1-3 digits
before the decimal point, then a decimal point only if the next
character is a digit (the lookahead clause), and then 0-3 digits after
the decimal point.
I've gotten around this problem with other javascript code around
the regex, but I'd just like to know why this "clean" solution doesn't
work.
Thanks for your help. This is a great group, I've learned so much
from the very knowledgable people here (including much regex stuff!)

The look-ahead is not supported on all browsers, as I heard whispering.
You probably would not want a leading 0, if >=10

<script type='text/jscript'>

function t(n){
return /^(([1-9]\d{0,2})||\d)(||(\.\d{1,3}))$/.test(n)
}

alert(t('.1')); // false
alert(t('1234')); // false
alert(t('123')); // true
alert(t('012')); // false
alert(t('123.')); // false
alert(t('2.77')); // true
alert(t('0.770')); // true
alert(t('0.7700')); // false

</script>


Sorry, i meant to TEST, not edit the entry. I just want true or
false, meaning valid or not valid.
 
E

Evertjan.

Evertjan. wrote on 08 jun 2006 in comp.lang.javascript:
function t(n){
return /^(([1-9]\d{0,2})||\d)(||(\.\d{1,3}))$/.test(n)
}

Following Martin's better syntax:

function t(n){
return /^(([1-9]\d?\d?)||0)(\.\d{1,3})?$/.test(n)
}

There are many ways to Rome.
 
B

bruce

Martin said:
Can't you simply use e.g.
var pattern = /^\d{1,3}(\.\d{1,3})?$/;
That allows one to three digits followed by nothing or by a decimal
point followed by one to three digits.
Still in real life you usually don't want e.g.
000
which that regular expression would allow.


Thank you! I didn't even need the lookahead! See, the simplest
solutions are the best. Thanks again.
 
B

bruce

Evertjan. said:
Evertjan. wrote on 08 jun 2006 in comp.lang.javascript:
function t(n){
return /^(([1-9]\d{0,2})||\d)(||(\.\d{1,3}))$/.test(n)
}

Following Martin's better syntax:

function t(n){
return /^(([1-9]\d?\d?)||0)(\.\d{1,3})?$/.test(n)
}

There are many ways to Rome.

Thanks for the complete answer!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top