0

I fail to extract a (double) number from a pre-defined input String using a regular expression.
The String is:

String inputline ="Neuer Kontostand";"+2.117,68";

For successfully parsing just the number I need to suppress the leading + while keeping an optional -. In addition I must cut off the " before/after the number.

Of course I could do multi-step string-operations, but does anyone know how to do all in a more elegant way using one regular expression?

What I tried so far:

Pattern p = Pattern.compile("-{0,1}[0-9.,]*");
Matcher m = p.matcher(inputline);
String substring =m.group();
Nightfirecat
  • 11,432
  • 6
  • 35
  • 51
user2260250
  • 1
  • 1
  • 1
  • 1
  • surround the part you want to extract as group with brackets ([0-9.,]*) – danieln Apr 09 '13 at 06:27
  • 1. `{0,1}` can be written as `?`; 2. your regex will match many things that are not numbers (e.g. `-` or `...` or even the blank string). – NPE Apr 09 '13 at 06:28

2 Answers2

4
Pattern.compile("-?[0-9]+(?:,[0-9]+)?")

Explanation

-?        # an optional minus sign
[0-9]+    # decimal digits, at least one
(?:       # begin non-capturing group
  ,       #   the decimal point (German format)
  [0-9]+  #   decimal digits, at least one
)         # end non-capturing group, make optional

Note that this expression makes the decimal part (after the comma) optional, but does not match inputs like -,01.

If your expected input always has both parts (before and after the comma) you can use a simpler expression.

Pattern.compile("-?[0-9]+,[0-9]+")
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • That's better, but doesn't address the problem that the OP is not *using* the regex anywhere :) – Tim Pietzcker Apr 09 '13 at 06:36
  • @Tim That's... well. Not part of the question. ;) – Tomalak Apr 09 '13 at 06:37
  • Well, not really. The OP only stated that he failed to extract the number correctly. His regex *would* have worked (somewhat), but he never used it. So that's his actual problem. – Tim Pietzcker Apr 09 '13 at 06:39
  • @Tim With respect to the mind-boggling abundance of Java regex usage examples I'd say mentioning this fact is more than enough. – Tomalak Apr 09 '13 at 06:42
  • 1
    Thanks! This was very fast and my regex now works fine. I only expanded the first group in your solution to [0-9.] to include the grouping separator (German format). My key problem in all my many tries was to use the star-sign instead of the plus-sign after the [0-9]. Thanks very much! – user2260250 Apr 09 '13 at 08:27
  • @user: If you want to make sure that only three digit groupings are accepted and the dot cannot be at the beginning, you can use use `"(?<![.,])\\b[0-9]{1,3}+(?:.[0-9]{3})*(?:,[0-9]+)?\\b"`. I leave figuring that expression out as an exercise to you. :) It contains a few advanced concepts that worth figuring out. Also see http://fiddle.re/xkcg6, click the "Java" button. – Tomalak Apr 09 '13 at 09:05
0
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumRegEx {

   public static void main( String[] args ) {
      String inputline = "\"Neuer Kontostand\";\"+2.117,68\"";
      Pattern p = Pattern.compile(".*;\"(\\+|-)?([0-9.,]+).*");
      Matcher m = p.matcher( inputline );
      if( m.matches()) { // required
         String sign  = m.group( 1 );
         String value = m.group( 2 );
         System.out.println( sign );
         System.out.println( value );
      }

   }
}

Output:

+
2.117,68
Aubin
  • 14,617
  • 9
  • 61
  • 84