0

I'm trying to create a regular expression in Java to validate a number with the following constraints:

  1. The number can be of any length but can only contain digits
  2. First digit can be 0 - 9
  3. Subsequent digits can be 0 - 9, but one of the digits must be non-zero.

For example: 042004359 is valid, but 0000000000 is not.

3
  • 1
    Please leave reason for downvote Commented Aug 13, 2012 at 21:47
  • 1
    Is 0 valid? You say that the number can be any length and the first digit can be 0-9, and apply the restriction that "one of the digits must be non-zero" in the sentence referring to "subsequent digits", but it seems likely you intended that no all-zero number would be valid. Commented Aug 13, 2012 at 21:59
  • Per @Keppil's comment on LouisWasserman's answer below, is #3 intended to restrict the subsequent digits to having at least one non-zero digit even if the first digit is non-zero? Commented Aug 13, 2012 at 22:01

4 Answers 4

3

\\d+[1-9]\\d* should work, I'd think.

Sign up to request clarification or add additional context in comments.

4 Comments

Change the first * to a +, and you are home.
I disagree, actually. If I changed the first * to a +, then this would only accept numbers with at least 2 digits, which I don't think is correct. (As it stands, the [1-9] is mandatory, so that forces at least one digit to be present.)
According to the specs, one of the numbers after the first one has to be non-zero, but your pattern accepts 10000 for example. This also implies that the number is at least of length 2.
Hrrrrm. I read the spec as "one of the digits in the whole string must be nonzero," but I suppose that reading makes more sense.
1

This should do what you need:

/^(?=.*[1-9])([0-9]+)$/

Whilst it matches all of digits [0-9] it contains a lookahead that makes sure there is at least one of [1-9].

I am fairly certain that Java allows can use lookaheads.

EDIT: This regular expression test page seems to imply that it can.

EDIT: If 0 is valid, then you can use this:

^((?=.*[1-9])([0-9]+)|0)$

This will make an exception for 0 on its own (notice the OR operator).

Comments

0
^(\d{1})(\d*?[1-9]{1}\d*)*$

^(\d{1}) - Line must start with 1 digit
(\d*?[1-9]{1}\d*)*$ - Line must end with zero or more 0-9 digits(? for conservative), then 1 1-9 digit, then zero or more digits. This pattern can repeat zero or more times.

Works with:

100000
100100
1010200
1
2

Maybe this is too complicated, lol.

Comments

0

Here's one solution using lookarounds: (?<=\D|^)\d+(?=[1-9])\d*

(?<=\D|^)   # lookbehind for non-digit or beginning of line
\d+         # match any number of digits 0-9
(?=[1-9])   # but lookahead to make sure there is 1-9
\d*         # then match all subsequent digits, once the lookahead is satisfied

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.