You can use this regex (([0-9]+\s?)+)\$
with pattern like this, which mean one or more degit can be followed by a space and all that end with curency sign $
:
String text = "player number 8 have a nice day. the price is 1 000 $ or you have to pay 2000$.";
String regex = "(([0-9]+\\s?)+)\\$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
result
1 000
2000
regex demo
Edit
What i have to write in regex when i have two currency? 1000$ and
1000EUR
In this case you can use this regex instead (([0-9]+\s?)+)(\$|EUR)
which can match both $
sign and EUR
String regex = "(([0-9]+\\s?)+)(\\$|EUR)";
regex demo 2
Edit2
I tested this and i find another trap. When i have 2000,00 $ and 1
000,00 EUR i get 00,00. So what i have to add to regex that give me
2000,1000?
So final example: I have : 1000$ and 5 000 EUR and 2 000 , 00 $ and
3000,00 EUR And output should be: 1000,5000,2000,3000, any regex for
this?
In this case you can use this regex (([0-9]+[\s,]*)+)(\$|EUR)
which can allow space and comma between numbers, then when you get the result you can replace all non degit with empty like this :
String text = "1000$ and 5 000 EUR and 2 000 , 00 $ and 3000,00 EUR";
//1000,5000,2000,3000
String regex = "(([0-9]+[\\s,]*)+)(\\$|EUR)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group(1).replaceAll("[^0-9]", ""));
// ^^^^^^--------to get only the degits
}
Output
1000
5000
200000
300000
regex demo 3