0

I need to ectract a double out of a String. The problem is, that the String looks like this:

"13,05 €"

Now I tried to extract just the number, using regex.

Double.parseDouble(test.getAmount().replaceAll("\\D+", ""));

As u can see, there is one problem. By using \D+ the ',' also will be replaced and I don't get the correct amount. Now I thought about how to fix this. Guess I need something like: "Replace all non-digits, but ,". But I can't figure out, how to express that with regex. Does someone have an idia?

4

1 回答 1

0

使用否定字符类:

Double.parseDouble(test.getAmount().replaceAll("[^\\d,]+", ""));

[^\\d,]是一个否定字符类,因为它以^. 所以这个类匹配所有内容,但数字和逗号除外。

在 Regexr 上查看

于 2013-01-25T12:02:16.843 回答