我有一个字符串,我想提取其中的浮动.我已经使用matcher.group()函数成功完成了它,但我只想单独显示它们.这是我的代码
String regex="([0-9]+[.][0-9]+)";
String input= "You have bought USD 1.00 Whatsapp for 784024487. Your new wallet balance is USD 1.04. Happy Birthday EcoCash for turning 7years. Live Life the EcoCash Way.";
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(input);
while(matcher.find())
{
System.out.println("First float is "+matcher.group());
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的答案是:第一个浮点数是1.00第一个浮点数是1.04
但我想说:第一个浮点数是1.00秒浮点数是1.04
我怎么做?
小智 5
也许这样的事情?
int k = 1;
while(matcher.find())
{
System.out.println("Float " + k + " is "+matcher.group());
k++;
}
Run Code Online (Sandbox Code Playgroud)
这将输出如下内容:Float 1为1.00 Float 2为1.04
| 归档时间: |
|
| 查看次数: |
63 次 |
| 最近记录: |