Extract number from string using regex in java -
i fail extract (double) number pre-defined input string using regular expression.
 string is:
string inputline ="neuer kontostand";"+2.117,68";   for parsing number need suppress leading + while keeping optional -. in addition must cut off " before/after number.
of course multi-step string-operations, know how in more elegant way using 1 regular expression?
what tried far:
pattern p = pattern.compile("-{0,1}[0-9.,]*"); matcher m = p.matcher(inputline); string substring =m.group();      
pattern.compile("-?[0-9]+(?:,[0-9]+)?")   explanation
-? # optional minus sign [0-9]+ # decimal digits, @ least 1 (?: # begin non-capturing group , # decimal point (german format) [0-9]+ # decimal digits, @ least 1 ) # end non-capturing group, make optional
note expression makes decimal part (after comma) optional, not match inputs -,01. 
if expected input always has both parts (before , after comma) can use simpler expression.
pattern.compile("-?[0-9]+,[0-9]+")      
Comments
Post a Comment