-1

In a Java application I need to check if a given string

  • contains only digits 0-9 and
  • is exactly five digits long

My first attempt is this:

public static void main(String[] args) {

    String testString = "000000";
    String myPattern = "\\d{5}";

    Pattern validCharacterPattern = Pattern.compile(myPattern);
    Matcher matcher = validCharacterPattern.matcher(testString);
    boolean b = matcher.find();

    if (b) System.out.println("Valid");
    else System.out.println("Invalid");

}

However the above expression is also true for e.g. 123456. What do I need to change?

Robert Strauch
  • 12,055
  • 24
  • 120
  • 192
  • 2
    "*However the above expression is also true for e.g. `123456`*" doesn't seem true. `"123456".matches("\\d{5}")` returns `false` to me. – Pshemo Apr 15 '14 at 20:31
  • 1
    http://java-regex-tester.appspot.com/ Says that `\d{5}` works for matches. Perhaps you should paste your actual code, as this seems contrived. – Cruncher Apr 15 '14 at 20:31
  • word boundary regex `\\b`. See http://stackoverflow.com/questions/4975644/regular-expression-to-match-exactly-5-digits – Gus Apr 15 '14 at 20:32
  • 4
    I tested the above expression for 123456, just to be sure. It is not true. This question is in error, as is every single answer on this page. – Dawood ibn Kareem Apr 15 '14 at 20:33
  • Please red the documentation of String.matches: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#matches(java.lang.String) – donfuxx Apr 15 '14 at 20:36
  • 3
    @Robert It would be really nice (and would stop your question from getting closed) if you would read what everyone has been saying and post an edit, or delete the question. – Cruncher Apr 15 '14 at 20:40
  • Your code is working for me: http://jdoodle.com/a/65 (click execute) – Robin Apr 15 '14 at 20:41
  • I have reworked my question and provided the specific code snippet. – Robert Strauch Apr 15 '14 at 20:43
  • 2
    "What do I need to change?" You could go back to the version you posted the first time, which worked great! Or change `matcher.find` to `matcher.matches`. – ajb Apr 15 '14 at 20:43
  • Is it a must to use `matcher`? Can't you iterate all the characters and check one by one with a counter? – padawan Apr 15 '14 at 20:45
  • 5
    Funny thing is that answer to your current version of your question was its previous version :) – Pshemo Apr 15 '14 at 20:45
  • lol now suddenly using pattern matcher.. – donfuxx Apr 15 '14 at 20:47
  • 4
    It really annoys me when people post questions about code that's different from the code that's actually causing them the problem. "Tell me what I'm doing wrong, in the code that I _didn't_ post." – Dawood ibn Kareem Apr 15 '14 at 20:51
  • @DavidWallace At some level they have to edit it a little bit. They can't just dump a million code files. But when they minimize it for a question they should actually..... run it. – Cruncher Mar 22 '17 at 15:09

2 Answers2

7

For sake of completeness (even though the question has changed completely)

boolean b = matcher.find();

This will match if the regex is contained somewhere in the matching string. If you use matcher.matches you will get the expected behaviour, where it must match the ENTIRE string.

Alternatively you can skip the compile step(not recommended if this regex is going to be used several times.) altogether and just write:

String regex = "\\d{5}";
String test = "123456";
if(test.matches(regex)){ ... };

Which is essentially what you had in the original question.

Cruncher
  • 7,641
  • 1
  • 31
  • 65
1

You are wrong

However the above expression is also true for e.g. 123456. What do I need to change?

Its false for 123456.


Sample code:

    String s = "123456";
    String regex = "\\d{5}";
    if (s.matches(regex))
        System.out.println("found");
    else
        System.out.println("not found");

output

not found

Question has been edited. Now try with start and end in regex.

    String testString = "000000";
    String myPattern = "^(\\d{5})$";

    Pattern validCharacterPattern = Pattern.compile(myPattern);
    Matcher matcher = validCharacterPattern.matcher(testString);
    boolean b = matcher.find();

    if (b)
        System.out.println("Valid");
    else
        System.out.println("Invalid");

output:

Invalid
Braj
  • 46,415
  • 5
  • 60
  • 76
  • 4
    This regex means exactly the same as `"\\d{5}"` so this is not the solution. – Jesper Apr 15 '14 at 20:29
  • OK we always give hints. A hint is sufficient for an intelligent guy as you. – Braj Apr 15 '14 at 20:30
  • I tried it 4 digits that why I posted it. I was just changing it but some one has done it already. – Braj Apr 15 '14 at 20:31
  • @Jesper try it yourself. – Braj Apr 15 '14 at 20:32
  • @Braj try this with `"123456".matches(regex)`, you'll see the result is `true`, exactly the problem that the original poster has - this will not match only exactly 5 digits. – Jesper Apr 15 '14 at 20:33
  • Have a look at my post. – Braj Apr 15 '14 at 20:34
  • @Jesper **Sorry its returning `false`** – Braj Apr 15 '14 at 20:35
  • What nonsense what is wrong. Can anyone explain me? – Braj Apr 15 '14 at 20:36
  • 2
    Ok, I tried it, the result is indeed `not found`. But if you try it with `"\\d{5}"` the result is also `not found`. Looks like the original question asker has something wrong. – Jesper Apr 15 '14 at 20:36
  • OP wants : **However the above expression is also true for e.g. 123456. What do I need to change?** – Braj Apr 15 '14 at 20:37
  • 1
    Well, as we can see, the expression is **not** true for 123456. The original asker of the question is wrong. – Jesper Apr 15 '14 at 20:38
  • Yes that what I want to know. Am I taking it wrong? – Braj Apr 15 '14 at 20:39
  • Yeah the question is wrong, that is why I voted to close the question. However, I don't see the point why `\\d{5}` should be replaced with `\\d\\d\\d\\d\\d` ==> it matches exactly the same, but it is just more tedious... what if you want to match exactly 25 digits, 200 digits etc. ;-) – donfuxx Apr 15 '14 at 20:45
  • I thought as per OP its not working then I tried with this pattern but later It come to know that [you know it.]:) – Braj Apr 15 '14 at 20:48
  • @Braj Assuming that the OP has run the code that he posted in a stackoverflow question is completely unreasonable. /sarcasm – Cruncher Apr 15 '14 at 20:55
  • 1
    @Cruncher I don't want to discuss more on this matter. I think its enough. :) – Braj Apr 15 '14 at 20:56