1

I want to extract the amount in Euro out of a string via a regular expression.

Presently I get only 5 as a result and cannot understand my error. How has a suitable solution to look like to detect also variants like 17,05 Euro or 85 EUR in my string?

    String regExp = ".*([0-9]+([\\,\\.]*[0-9]{1,})?) *[Eu][Uu][Rr][Oo]? .*";
    Pattern pattern = Pattern.compile(regExp);

    String input1 = "aerae aerjakaes jrj kajre kj 112123 aseraer 1.05 Eur aaa";
    Matcher matcher = pattern.matcher(input1);
    matcher.matches();
    System.out.println(matcher.group(1));

Result:

5

2 Answers2

2

You only get 5 because the first .* is greedy and grabs the whole line at first, then backtracks yielding character by character until the subsequent subpatterns match. That is why the last digit is only captured since only 1 is required by your pattern.

You may use a simpler pattern with Matcher#find:

String regExp = "(?i)([0-9]+(?:[.,][0-9]+)?)\\s*euro?";
Pattern pattern = Pattern.compile(regExp);
String input1 = "aerae aerjakaes jrj kajre kj 112123 aseraer 1.05 Eur aaa";
Matcher matcher = pattern.matcher(input1);
if (matcher.find()) {
    System.out.println(matcher.group(1));
}

See the Java demo

  • (?i) - case insensitive modifier (no need to write [eE][Uu]...)
  • ([0-9]+(?:[.,][0-9]+)?) - Group 1:
    • [0-9]+ - 1 or more digits
    • (?:[.,][0-9]+)? - an optional sequence of:
      • [.,] - a literal . or , symbols
      • [0-9]+ - 1 or more digits
  • \\s* - 0+ whitespaces
  • euro? - a eur or euro substring.

You may even reduce [0-9]+(?:[.,][0-9]+)? to [0-9][.,0-9]* subpattern to match a digit followed with 0+ digits, . or , if the text is written well.

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

Replace:

String regExp = ".*([0-9]+([\\,\\.]*[0-9]{1,})?) *[Eu][Uu][Rr][Oo]? .*";
Pattern pattern = Pattern.compile(regExp);

String input1 = "aerae aerjakaes jrj kajre kj 112123 aseraer 1.05 Eur aaa";
Matcher matcher = pattern.matcher(input1);
matcher.matches();
System.out.println(matcher.group(1));

With:

String regExp = "(?i)\\d*\\.*,*\\d*\\s(euro?)";
Pattern pattern = Pattern.compile(regExp);
String input1 = "aerae aerjakaes jrj kajre kj 112123 aseraer 1.05 Eur aaa";
Matcher matcher = pattern.matcher(input1);
if(matcher.find()) {
    System.out.println(matcher.group(0));

}

This works for the variants you provided.

walyzfan1
  • 127
  • 1
  • 1
  • 7