We help IT Professionals succeed at work.

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

aturetsky
aturetsky asked
on
513 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?
Comment
Watch Question

Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
Like this (doesn't allow leading zeroes):
[1-9][0-9]*
Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
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]*$

Author

Commented:
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?
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.

Author

Commented:
ghostdog74:  I don't want to do it via exception handling (I am using java).
CERTIFIED EXPERT
Top Expert 2016

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

Open in new window

Maciej Ssysadmin
CERTIFIED EXPERT

Commented:

Author

Commented:
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?
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.