Solved

simplest way to check if a given string is a positive integer using regex

Posted on 2009-05-04
10
494 Views
Last Modified: 2012-05-06
This seems to be simple and yet I can't find anything other than complicated expressions I found on other sites.

Basically I need a regular expression that will check if a given string is an integer 1 or above.  So, for example, [1-9] would ensure that it's an integer 1 through 9.  But how would I check for an integer from 1 and up?
0
Comment
Question by:aturetsky
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 24300353
Like this (doesn't allow leading zeroes):
[1-9][0-9]*
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 24300364
You'll probably want to make sure you don't match on a partial string too actually, so you'll need to match the start and end of the string with ^ and $ respectively:
^[1-9][0-9]*$
0
 
LVL 1

Author Comment

by:aturetsky
ID: 24300552
Hmm, your first solution seems to work as well and doesn't appear to match on a partial string (which is as desired).   Does the need for ^ and $ depend on what language and api is being used?  I am using java and am doing

inputString.matches([1-9][0-9]*)

and it (as desired) returns false for partial matches, so for "blah45" or "45blah" it returns false, but for "45" it returns true.

Why then would we need the begin and end characters?
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 9

Expert Comment

by:ghostdog74
ID: 24300728
this is simple you don't need regular expression. convert the string to number (if necessary, if not depending on your language will do it for you automatically), then check if its greater than 0, eg  if int(string) > 0. i can't see why you need a regular expression for that simple task.
0
 
LVL 1

Author Comment

by:aturetsky
ID: 24300799
ghostdog74:  I don't want to do it via exception handling (I am using java).
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24302107
You can simplify that somewhat to
boolean isPositive = s.matches("\\d+");

Open in new window

0
 
LVL 23

Expert Comment

by:Maciej S
ID: 24302273
0
 
LVL 1

Author Comment

by:aturetsky
ID: 24305090
CEHJ - that looks like the best solution thus far - but it doesn't exclude 0 and, as mentioned, I need 1 and up.  Any thoughts?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 24305839
Sorry - missed that. Of course you could do the following and save yourself one character over the first post ;-) (You don't need to worry about partial matching - it's not allowed)


inputString.matches("[1-9]\\d*");

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24764769
:-)
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question