【发布时间】:2016-06-18 00:41:28
【问题描述】:
如何在此处应用过滤器
String x=Showing 138 of 138 String(s)
我只想得到138,结束值如何得到?
【问题讨论】:
-
使用正则表达式匹配模式
标签: java
如何在此处应用过滤器
String x=Showing 138 of 138 String(s)
我只想得到138,结束值如何得到?
【问题讨论】:
标签: java
你可以试试:
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);
【讨论】:
s.split(" "); ==> 你正在使用正则表达式:)