-1

I've got a problem with a regex, I need to extract a string from text and keep whitespace, coma and dot in the number(if it is exists), example: Lorem impsum the price is:196 000,2545.20 $ blablabla, Output:
196 000,2545.20

Example2:2530002545.20 £, Output:
2530002545.20

I did the following:

String exampleStr = "Lorem impsum the price is:196 000,2545.20 $ blablabla"
String result = exampleStr.replaceAll("[^\\d.,]", " ")
                          .replaceAll(/\s*$/,'')

Firstly replace everything that is not whitespace, comma, dot or whitespace(to keep it in the number), secondly removed spaces from the end of the string. This solution works, BUT I want to re-do it in a more proper way, to keep that white space inside and remove others from the beginning and the end of the 'price'. Ps: I really don't understand how to combine these two regexes, looking forward to your answers. Peace!

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

2 Answers2

2

This simple regular expression \d([\d., ]*\d)? directly ignores leading and trailing text and spaces, and keeps dots, commas and spaces that are between the digits.

The Java string for this RegExp is "\\d([\\d., ]*\\d)?".

Regards.

Éric
  • 437
  • 3
  • 9
1

Since you need to extract a substring matching the regex you can use Matcher. To get rid or trailing space just trim the matching substring. Try this out:

        String exampleStr = "Lorem impsum the price is:196 000,2545.20 $ blablabla2:2530002545.20 £";
        Pattern pattern = Pattern.compile("\\d[\\s\\d.,]+");
        Matcher matcher = pattern.matcher(exampleStr);
        while (matcher.find()) {
            System.out.println("'" + matcher.group().trim() + "'");
        }

Output:

'196 000,2545.20'
'2530002545.20'