Help with an "Easy" regular expression

hefterr
hefterr used Ask the Experts™
on
Hi,
I am a newbie to REGEX but the web world seems to have problems with a simple REGEX:

I just need to validate an input string to make sure they all consist of integers ((characters 0,1,2,3,4,5,6,7,8,9).  No leading or traling spaces allowed either.  Any number of integers allowed.  You can also assume the input is not all spaces if that helps as I test for that first.

FYI.  I am using Coldfusion using the IsValid("regex",variable, etc)

I found on a Coldfusion site  that recommended : ^{0,1}[1-9]+[\d]*  - but it does not allow the string to begin with "0" and also allows for a leading space?

Your expertise is greatly appreciated.

hefterr
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Allows only Numeric numbers
\d*$
^\d+$
Prasad's answer allows leading spaces and also, empty string
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Most Valuable Expert 2015

Commented:
(no points ...)

+1 to @mgh_mgharish's suggestion.  

The key is ^ (starts with) and $ (ends with).  In other words the value must consist entirely of digits.

Commented:
Allows only Numeric numbers
\d*$

This allows only Numeric number. It does not allow any spaces.

Author

Commented:
Hey guys, ^\d+$ is also allowing leading  and trailing spaces :

<cfset mytest=" 00800000 ">
<cfoutput>
<cfif isValid("regex", mytest, "^\d+$")>
**#mytest#** is valid!!!
<cfelse>
**#mytest#** is NOT  valid!!!
</cfif>
</cfoutput>

I am adding a does not contains " " until someone can creack this one!

hefterr
What if you use ReFind() instead of isValid??

ReFind("^\d+$", mytest)
Most Valuable Expert 2015

Commented:
(again no points....)

Something is hinky with isValid(...) I think it may be trimming the value first. Because the same expression gives different results with ReFind() and IsValid().
Most Valuable Expert 2015

Commented:
>> It does not allow any spaces.

    Actually, it does.  At least when IsValid doesn't mangle it anyway ...

    <cfif ReFind("\d*$", " 9")>
        found
    <cfelse>
        not found
    </cfif>
Zvonko זְאֵבSystems architect
Top Expert 2006

Commented:
Look at this page:
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=validateData_12.html

Therefore you have to implement the start and end chars yourself:

<cfset mytest="00800000">
<cfoutput>
<cfif isValid("regex", "^#mytest#$", "\^\d+\$")>
**#mytest#** is valid!!!
<cfelse>
**#mytest#** is NOT  valid!!!
</cfif>
</cfoutput>


Or even more simple:
<cfif isValid("integer", mytest) >


If you want to allow empty strings then use * instead of +
<cfif isValid("regex", "^#mytest#$", "\^\d*\$")>



Author

Commented:
Hey guys,
I think  :
<cfif ReFind("^\d+$", mytest)>

 seems to work fine for me!!!  IsValid has issues!!  FYI, the reason I don't use
<cfif isValid("integer", mytest) > is because CF treats it as a number an has a max number of character (not a lot either) before it throws an error.

Any last thoughts?  I think it's just using ReFind versus Isvalid?

hefterr


Most Valuable Expert 2015

Commented:
>> Any last thoughts?  I think it's just using ReFind versus Isvalid?

    Yeah, integer has a specific meaning and it's more than just "all digits". "Integer" enforces
    a specific range check which includes negative numbers.  So if your input doesn't share those
    rules it's better to use ReFind() instead.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial