Extract number from string using regex in java

前端 未结 2 1921
星月不相逢
星月不相逢 2021-01-29 05:36

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.11         


        
2条回答
  •  离开以前
    2021-01-29 05:49

    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
    

提交回复
热议问题