Reputation: 13
String s = "PING www.google.com (173.194.70.106) 56(84) bytes of data."
boolean z = pattern.matches("\\(([0-9]{1,3}\\.){3}[0-9]{1,3}\\)", s);
z will be 0. Where am I making a mistake given that the sequence that I am trying to detect is (X.X.X.X) so for s
that would be (173.194.70.106)
?
egrep -e "\(([0-9]{1,3}\.){3}[0-9]{1,3}\)"
test gives the desired result so I assume it's a problem between me and java.
Upvotes: 1
Views: 243
Reputation: 3534
matches()
will try to match the whole string on the regular expression.
For your purpose you could use Matcher.find()
:
String s = "PING www.google.com (173.194.70.106) 56(84) bytes of data.";
Pattern p = Pattern.compile("\\(([0-9]{1,3}\\.){3}[0-9]{1,3}\\)");
Matcher m = p.matcher(s);
if (m.find()) {
System.out.println(m.group(0));
}
Upvotes: 3