4

I made a regular expression for checking the length of String , all characters are numbers and start with number e.g 123 Following is my expression

REGEX =^123\\d+{9}$";

But it was unable to check the length of String. It validates those strings only their length is 9 and start with 123. But if I pass the String 1234567891 it also validates it. But how should I do it which thing is wrong on my side.

2
  • 1
    Get rid of the +: REGEX =^123\\d{6}$"; Commented Feb 19, 2013 at 12:58
  • The word validate better describes what you're trying to accomplish. You're trying to validate the length of the string to contain exactly 9 digits after the prefix "123". Commented Feb 19, 2013 at 13:00

3 Answers 3

6

Like already answered here, the simplest way is just removing the +:

^123\\d{9}$

or

^123\\d{6}$

Depending on what you need exactly.

You can also use another, a bit more complicated and generic approach, a negative lookahead:

(?!.{10,})^123\\d+$

Explanation:

This: (?!.{10,}) is a negative look-ahead (?= would be a positive look-ahead), it means that if the expression after the look-ahead matches this pattern, then the overall string doesn't match. Roughly it means: The criteria for this regular expression is only met if the pattern in the negative look-ahead doesn't match.

In this case, the string matches only if .{10} doesn't match, which means 10 or more characters, so it only matches if the pattern in front matches up to 9 characters.

A positive look-ahead does the opposite, only matching if the criteria in the look-ahead also matches.

Just putting this here for curiosity sake, it's more complex than what you need for this.

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

2 Comments

your (?!.{10,})^123\\d+$ it is working fine thanks can you elaborate it
@pcalcao : I want to check if string starts with number and also if it contains special characters. I was able to right regex for special characters /[\.,\/\\#+!%\^&*;:{}=`~()\s]/ Now I want to add check if string starts with number too in this regex. How I can achieve it?
5

Try using this one:

^123\\d{6}$

I changed it to 6 because 1, 2, and 3 should probably still count as digits.

Also, I removed the +. With it, it would match 1 or more \ds (therefore an infinite amount of digits).

2 Comments

My requirement is any number start with specific number like 123 or 45 or 67 any thing That mean every time I have to change length parameter depends upon user send me in parameter
@waseem Then check str.length() == 9. However, that wasn't in your original question. Ask a new question if you still have problems.
0

Based on your comment below Doorknobs's answer you can do this:

int length = 9;
String prefix = "123"; // or whatever
String regex = "^" + prefix + "\\d{ " + (length - prefix.length()) + "}$";
if (input.matches(regex)) {
    // good
} else {
    // bad
}

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.