Regular expression for decimal value

M

Markus Joschko

Hi all,
I'm not really a pro concerning regular expressions and in the moment I
desperatly try to get a decimal check working.

There is a kind of a textinputfield which should contain a decimal number.
The only restriction is the length of the overall number (excluding
period):

E.g. length=5

888888 invalid
88888 is valid
88.888 is valid
888.88 is valid
8.8888 is valid
8888.8 is valid

And that is the problem for me.

(\\d*(\\.\\d*){0,1}) -> no length check

(\\d*(\\.\\d*){0,1}){0,5}? -> checks how often the group occurs.

But how can I restrict the overall count of digits ( it does not really
matter if the period is excluded but would be fine)?

Greetings,
Markus
 
M

Mark Clements

Purl said:
This number you display is not a decimal number.
You are contradicting yourself. State clear,
concise and coherent parameters.

88888 is a base 10 number. Looks pretty decimal to me.

Mark
 
T

Tad McClellan

Markus Joschko said:
I'm not really a pro concerning regular expressions


That's fine, since you don't need to write any regexes yourself
to solve your problem.

There is a kind of a textinputfield which should contain a decimal number.


perldoc -q number

How do I determine whether a scalar is a number/whole/integer/float?

The only restriction is the length of the overall number (excluding
period):

E.g. length=5


/something from FAQ answer/ and tr/0-9// <= 5; # untested
 
M

Mark Clements

Purl said:
Oh my, another one of those who believe
a whole number is a decimal number.
indeed.

Markus: try something like

use strict;
use warnings;

my $testnum = shift;
my $maxlen = shift;
{
my $tempnum = $testnum;
if ($tempnum=~/\b\d+(\.\d+)?\b/ && $tempnum=~s/\d//g <= $maxlen){
print "pass\n";
}
}
 
L

Lukas Mai

Markus Joschko schrob:
Hi all,
I'm not really a pro concerning regular expressions and in the moment I
desperatly try to get a decimal check working.
There is a kind of a textinputfield which should contain a decimal number.
The only restriction is the length of the overall number (excluding
period):
E.g. length=5
888888 invalid
88888 is valid
88.888 is valid
888.88 is valid
8.8888 is valid
8888.8 is valid

/^ \d (?: \.\d{3} | \d\.\d{2} | \d{2}\.\d | \d{3}\.? ) \d \z /x

But does it have to be a single regex?

/^(\d+)(?:\.(\d+))?\z/ and length($1) + (defined $2 ? length($2) : 0) <= 5

Hmm, that's not really better.

/^\d+(?:(\.)\d+)?\z/ and length($_) - !!$1 <= 5

That's quite readable IMHO, even though the !! part is a bit obfuscated.

HTH, Lukas
 
P

Paul Lalli

Markus Joschko wrote:

(snipped)




"...should contain a decimal number."


Review your primary education level arithmetic rules.

Please review your grasp of the english language.

The American Heritage Dictionary of the English Language, Fourth Edition:
decimal, n. :
A number written using the base 10


"decimal number" != "a number containing a decimal point"

Paul Lalli
 
M

Mark Clements

Purl said:
Markus Joschko wrote:
"...should contain a decimal number."
88888 is valid
Review your primary education level arithmetic rules.

88888 is a number written (in this case) using the decimal system ie in
base 10. It is therefore a decimal number, and has zero decimal places.
You are, of course, free to dispute that 88888 is written in base 10 for
the purposes of this thread, and that zero is a valid number of decimal
places for a decimal number to have.

Mark
 
M

Mark Clements

Purl said:
Do extract a precise quote from the originating author's
article which states use of base 10 numbers.
Er - he said "decimal number". If you choose to have a different
definition of "decimal number" to everybody elses definition, then that
is your choice. Just as a matter of interest, what is your definition of
"hexadecimal number"?
You boys are attempting to cover for your lack of education,
with mule manure.
Got it in one.
Yes, Socratic Irony works well with the less-than-educated.
Probably, but for those who have to deal with the idiocy of others on a
day-to-day basis it's a little tiring.

Mark
 
P

Paul Lalli

Mule manure.

HAHAHAH. Okay, thank you for that. I needed a laugh. When someone
refers you to the documentation (in the case of the English language, a
dictionary) that flat out contradicts you, you not only responded with a
childish comment, but omitted the relevant citation. Next you'll tell me
"nuh-uh!!!", stick out your toungue, and give me the raspberries.

Again, thanks for the humor value of your post.

Paul Lalli
 
P

Paul Lalli

Do extract a precise quote from the originating author's
article which states use of base 10 numbers.

The original poster used the term "decimal", which has a dictionary
definition of "base 10 number". Just to drive home the point, he then
listed several examples of what constituted valid input for his program.
This valid input corresponded perfectly well with said dictionary
definition.

You have chosen to first tell the original poster he doesn't know what
'decimal' means, then tell the original poster he doesn't know what kind
of input he actually wants, then claim that the dictionary definition is
wrong, apparently just because you say so.

Do you have any intention of backing up anything you say with actual facts
or references? Or do you just not know how to handle being wrong?

Paul Lalli
 
M

Markus Joschko

Mark said:
use strict;
use warnings;

my $testnum = shift;
my $maxlen = shift;
{
my $tempnum = $testnum;
if ($tempnum=~/\b\d+(\.\d+)?\b/ && $tempnum=~s/\d//g <= $maxlen){
print "pass\n";
}
}

Thanks Clements, Lukas, Ted for the constructive input.
To the rest: I always like to learn more. But to avoid misunderstandings
caused by my wrong usage of english terms I have written down some
examples. I thought they were clear enough to grasp what I mean and need.
OK, 8888 might not be a decimal number/fraction or whatever, but I won't
tell the user to enter 8888.0 just because it is the correct scientific
usage.

BTW, I need one regular expression because the number is checked by a plugin
which is based on regexs. Otherwise I would simply check the length of the
expression.

Greetings,
Markus
 
T

Tad McClellan

Markus Joschko said:


It is NOT "by the way", it appears to be an essential requirement
that was not shared with us...

I need one regular expression because the number is checked by a plugin
which is based on regexs. Otherwise I would simply check the length of the
expression.


Poor specs yield poor implementations.

The corollary then is: Take great care in preparing your specification.

I wasted time providing an unusable solution. I could have spent
that time helping someone else.
 
M

Matt Garrish

Paul Lalli said:
Or do you just not know how to handle being wrong?

You hit the nail on the head with that one. Her ego exceeds her intellect.
Although no fan of killfiles, I usually just skim her messages for the words
"mule", "boys" and (albeit less prevalent of late) "sissified". If any
appear in her posting the rest of the thread isn't worth reading (except if
I'm looking for a laugh as she is proven wrong again and again).

Matt
 
G

Glenn Jackman

Markus Joschko said:
There is a kind of a textinputfield which should contain a decimal number.
The only restriction is the length of the overall number (excluding
period):

E.g. length=5

888888 invalid
88888 is valid
88.888 is valid
888.88 is valid
8.8888 is valid
8888.8 is valid

Here's a regex that's probably wildly inefficient, but will match a
number with at most 5 digits:

$re = qr/^(?:\.\d{1,5}|\d(?:\.\d{0.4}|\d(?:\.\d{0,3}|\d(?:\.\d{0,2}|\d(?:\.\d?|\d\.?)))))$/;

or, verbosely

$re = qr{
^ (?: # at the beginning, match:
\.\d{1,5} # a dot and 1 to 5 digits
| # or
\d (?: # a digit followed by
\.\d{0.4} # a dot and up to 4 digits
| # or
\d (?: # a digit followed by
\.\d{0,3} # a dot and up to 3 digits
| # or
\d (?: # a digit followed by
\.\d{0,2} # a dot and up to 2 digits
| # or
\d (?: # a digit followed by
\.\d? # a dot and an optional digit
| # or
\d\.? # a digit and an optional dot
))))) $ # and the end of the string
}x;
 
C

Chris Mattern

Purl said:
Mule manure.
Does this mean a hexidecimal number has to have
a hexidecimal point?
--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
S

Salvador Fandiño

Markus said:
Hi all,
I'm not really a pro concerning regular expressions and in the moment I
desperatly try to get a decimal check working.

There is a kind of a textinputfield which should contain a decimal number.
The only restriction is the length of the overall number (excluding
period):

E.g. length=5

888888 invalid
88888 is valid
88.888 is valid
888.88 is valid
8.8888 is valid
8888.8 is valid

And that is the problem for me.

(\\d*(\\.\\d*){0,1}) -> no length check

(\\d*(\\.\\d*){0,1}){0,5}? -> checks how often the group occurs.

But how can I restrict the overall count of digits ( it does not really
matter if the period is excluded but would be fine)?

Greetings,
Markus

this seems to work:

/^(?!.*\..*\.)(?:\d{5}|(?=.*\.)[\d\.]{6})$/

- Salva
 
S

Salvador Fandiño

I said:
this seems to work:

/^(?!.*\..*\.)(?:\d{5}|(?=.*\.)[\d\.]{6})$/

or even better

/^(?:\d{5}|(?=.{6}\Z)\d*\.\d*)$/

and if you don't want to accept .88888

/^(?:\d{5}|(?=.{6}\Z)\d+\.\d*)$/

- Salva
 
B

Bryan Castillo

Markus Joschko said:
Thanks Clements, Lukas, Ted for the constructive input.
To the rest: I always like to learn more. But to avoid misunderstandings
caused by my wrong usage of english terms I have written down some
examples. I thought they were clear enough to grasp what I mean and need.
OK, 8888 might not be a decimal number/fraction or whatever, but I won't
tell the user to enter 8888.0 just because it is the correct scientific
usage.

BTW, I need one regular expression because the number is checked by a plugin
which is based on regexs. Otherwise I would simply check the length of the
expression.

What is the name of the plugin? Is it a custom framework you are
working with or a third-party one? Perhaps the plugin framework has a
length attribute check in addition to a regular expression validation.

(Just out of curiosity is this even a perl framework you are working
with?)
 

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

Forum statistics

Threads
474,149
Messages
2,570,841
Members
47,388
Latest member
EarthaGilm

Latest Threads

Top