Numbers & Whitespace Help!

D

digitalje

Hi All,

Just a quick question...

Is there a regular expression that I could use that would fail if a
number has a white space(s)embedded in it?

For example:

1.5 <-- Should pass as a valid number
1 .5 <-- Should fail since there is a white space in between the 1
and decimal.

Any help would be greatly appreciated!

Thanks,
J E
 
S

Sherm Pendley

digitalje said:
Hi All,

Just a quick question...

Is there a regular expression that I could use that would fail if a
number has a white space(s)embedded in it?

For example:

1.5 <-- Should pass as a valid number
1 .5 <-- Should fail since there is a white space in between the 1
and decimal.

Any help would be greatly appreciated!

There are a number of docs included with Perl that explain regexes. Have a
look at:

perldoc perlrequick
perldoc perlretut
perldoc perlre

sherm--
 
J

Jürgen Exner

digitalje said:
Is there a regular expression that I could use that would fail if a
number has a white space(s)embedded in it?

For example:

1.5 <-- Should pass as a valid number
1 .5 <-- Should fail since there is a white space in between the 1
and decimal.

Well, that is not a number.
Do you mean "if a string contains a white space"? Sure, m/\s/
 
D

Dave

digitalje said:
Hi All,

Just a quick question...

Is there a regular expression that I could use that would fail if a
number has a white space(s)embedded in it?

For example:

1.5 <-- Should pass as a valid number
1 .5 <-- Should fail since there is a white space in between the 1
and decimal.

Any help would be greatly appreciated!

Thanks,
J E

See my answer to Regular Expression for Integer and float values below
 
D

digitalje

Found the expresssion, this seems to work:

^[0-9]*\.{0,1}[0-9]*$

Thanks to everybody.
 
P

Paul Lalli

digitalje said:
Shorter and neater:

^\d*\.{0,1}\d*$

You understand that each of the following would pass this test, right?

"5."
"."
".4"
".0000"
""

Paul Lalli
 
D

Dr.Ruud

digitalje schreef:
Is there a regular expression that I could use that would fail if a
number has a white space(s)embedded in it?

Why only reject internal whitespace and not other unwanted characters?

See Regexp::Common, more specifically Regexp::Common::number.
 
D

digitalje

Hey Paul,

Didn't think about the ".". Thanks for picking that up.

The other items are ok. I am converting the string into a number after
validating and the process is smart enough to know that:

5. --> 5.0
..4 --> 0.4

etc...

Thanks.
 

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

Similar Threads

Help please 8
Help with my responsive home page 2
Help with code plsss 0
Help with Loop 0
Help with passing test 3
I need help fixing my website 2
Code help please 4
Can't solve problems! please Help 0

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top