Reputation: 40358
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.
Upvotes: 2
Views: 1429
Reputation: 31035
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");
}
Upvotes: 2
Reputation: 727067
Although the pattern is the same, the input strings are different:
^...$
expression^
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.
Upvotes: 2