2
1)  Pattern pattern = Pattern.compile("34238");
   Matcher matcher = pattern.matcher("6003 Honore Ave Suite 101 Sarasota Florida,
   34238");
    if (matcher.find()) {
        System.out.println("ok");
    }

2)  Pattern pattern = Pattern.compile("^[0-9]{5}(?:-[0-9]{4})?$");
    Matcher matcher = pattern.matcher("34238");
    if (matcher.find()) {
        System.out.println("ok");
    }

Output for the above code is: ok

But the following code is not printing anything:

    Pattern pattern = Pattern.compile("^[0-9]{5}(?:-[0-9]{4})?$");
    Matcher matcher = pattern.matcher("6003 Honore Ave Suite 101 Sarasota Florida, 34238");
    if (matcher.find()) {
        System.out.println("ok");
    }

What is the reason for this not to print ok? I am using the same pattern here also.

6
  • Why not using "6003 Honore Ave Suite 101 Sarasota Florida, 34238".contains("34238") ? Commented Jul 20, 2016 at 15:35
  • It is US zip code format.We are not sure will get the same Commented Jul 20, 2016 at 15:35
  • Finally I have to find weather that sentence has US zip code or not Commented Jul 20, 2016 at 15:36
  • Which of the two blocks produces ok? Commented Jul 20, 2016 at 15:37
  • 1st and 2nd blocks Commented Jul 20, 2016 at 15:37

2 Answers 2

2

Although the pattern is the same, the input strings are different:

  • In your second example, you are matching a string consisting entirely of a zip code, so you get a match for ^...$ expression
  • The second example does not start with the zip code, so the ^ anchor prevents your regex from matching.

^ and $ anchors are used when you want your expression to match the entire input line. When you want to match at the beginning, keep ^ and remove $; when you want to match at the end, remove ^ and keep $; when you want to match anywhere inside the string, remove both anchors.

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

Comments

2

The code is good and working as expected. In the 2) and 3) block in your question you are using the same regex but different input strings.

However, if you just want to check if a string must contain a US zip code, then the problem is that your regex is using anchors, so you are only matching lines that starts and finish with a zip code.

The strings that matches your regex are like 34238 or 34238-1234 and won't match something 12345 something.

If you remove the anchors, then you will match whatever 12345 whatever:

// Pattern pattern = Pattern.compile("^[0-9]{5}(?:-[0-9]{4})?$");
//                                    ^--------- Here -------^
Pattern pattern = Pattern.compile("[0-9]{5}(?:-[0-9]{4})?");
Matcher matcher = pattern.matcher("6003 Honore Ave Suite 101 Sarasota Florida, 34238");
if (matcher.find()) {
    System.out.println("ok");
}

Btw, if you just want to check if a string contains a zip code, then you can use String.matches(..), like this:

String str = "6003 Honore Ave Suite 101 Sarasota Florida, 34238";
if (str.matches(".*[0-9]{5}(?:-[0-9]{4})?.*")) {
    System.out.println("ok");
}

IDEOne demo

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.