我试图使用模式来搜索字符串中的邮政编码.我无法让它正常工作.
inputLine的一个示例是
What is the weather in 75042?
Run Code Online (Sandbox Code Playgroud)
我试图用于模式的是
public String getZipcode(String inputLine) {
Pattern pattern = Pattern.compile(".*weather.*([0-9]+).*");
Matcher matcher = pattern.matcher(inputLine);
if (matcher.find()) {
return matcher.group(1).toString();
}
return "Zipcode Not Found.";
}
Run Code Online (Sandbox Code Playgroud)
如果我只想获得75002,我需要更改什么?这只输出数字2中的最后一位数字.我非常困惑,我不完全理解Pattern类的Javadoc.