小编Tho*_*ker的帖子

在不紧跟在数字之前的特定单词之后搜索数字

我试图使用模式来搜索字符串中的邮政编码.我无法让它正常工作.

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.

java regex

7
推荐指数
2
解决办法
128
查看次数

标签 统计

java ×1

regex ×1