Java 如何从字符串中筛选/提取数据?

Java 如何从字符串中筛选/提取数据?,java,Java,如何在这里应用过滤器 String x=Showing 138 of 138 String(s) 我只想得到138,结束值如何得到?您可以尝试: public static void main(String[] args) throws IOException { String x="Showing 158 of 138 String(s)"; System.out.println(x.replaceAll(".*\\s(\\d+).*", "$1")); } O/p:13

如何在这里应用过滤器

 String x=Showing 138 of 138 String(s)
我只想得到138,结束值如何得到?

您可以尝试:

public static void main(String[] args) throws IOException {
    String x="Showing 158 of 138 String(s)";
    System.out.println(x.replaceAll(".*\\s(\\d+).*", "$1"));
}

O/p:
138

我不会为此使用正则表达式,因为它不是灵活的解决方案。尝试对字符串进行配对以获取有效值:

String s =" String x=Showing 138 of 138 String(s)";
String[] words = s.split(" ");
BigDecimal value = null;
for(String word : words) {
    try {
        value = BigDecimal.valueOf(word);
        } catch(NumberFormatException e) {}
    }
System.out.println(value);

使用正则表达式匹配模式see
s.split(“”)=>您使用的是regex:)@TheLostMind足够近,但不是一个无法检测到double;)呃,OP没有说他要小数。