1

I want get number from String which have currency. For example:

String text = "player number 8 have a nice day. the price is 1 000 $ or you have to pay 2000$.";

So the output i want:

1000,2000

I use this:

String tmp = text.replaceAll("[^0-9]+", " ");
List<String> digitsList = Arrays.asList(tmp.trim().split(" "));

But my output is:

8,000,2000

Is any idea to get number from text if number is written like this: 1 000, 30 000.

And any way to get number only with currency ?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
JavaCoder
  • 135
  • 2
  • 8

1 Answers1

4

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

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • Your comeback :) – davidxxx Jul 30 '17 at 20:04
  • What i have to write in regex when i have two currency? `1000$ and 1000EUR` ? – JavaCoder Jul 30 '17 at 21:33
  • @Alcwak you have to use this regex in this case `(([0-9]+\\s?)+)(\\$|EUR)` hope this can help you :) – Youcef LAIDANI Jul 30 '17 at 21:37
  • Thank you. 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`? – JavaCoder Jul 30 '17 at 21:59
  • 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? – JavaCoder Jul 30 '17 at 22:32
  • yes @Alcwak you can check my edit hope this can help you :) – Youcef LAIDANI Jul 31 '17 at 06:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150582/discussion-between-alcwak-and-ycf-l). – JavaCoder Jul 31 '17 at 09:34
  • @Alcwak sorry i can't use chat room in my network if you have any question you can use comments :) – Youcef LAIDANI Jul 31 '17 at 09:39
  • Thanks for helping, now it works perfect. I have another question, maybye you know answer, since i'm looking for another regex: I have: `"The price is 1 000$. You have to pay 1400 EUR, and you have to pay extra 2000$". `What i want? I want price, but if before price is word "pay" or "pay extra" then i must reject this price. I have regex which you wrote that give me price, so it is great, but i think that i need another? or modify your regex that reject some price if before price is specjal word. Output of my example should be: `1000` – JavaCoder Jul 31 '17 at 10:48
  • @Alcwak it can be better if you create a new question instead explain every thing, just to make Q&A more helpful for other, when you create it inform me ok :) – Youcef LAIDANI Jul 31 '17 at 11:28
  • @YCF_L okey,this is the link : https://stackoverflow.com/questions/45415457/reject-price-before-special-word-regex-java – JavaCoder Jul 31 '17 at 12:03