Javaライブラリかどうかをチェック文字列が含まれています*なし-例外
-
18-09-2019 - |
質問
を探している方法を返しますbooleanの場合は文字列で渡しても有効な番号例"123.55e-9","-333,556").I な いただい:
public boolean isANumber(String s) {
try {
BigDecimal a = new BigDecimal(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
この機能を使用する必要がありますの状態機械(DFA)構文解析、文字列だ無効例のない馬鹿です(例"-21,22.22.2", "33-2").い場合には図書館が存在する?んですが、書きたいなので、自分で明らかな問題というスタイルを再発明をするつもりはありま
おかげさ
ニック
解決
私のような大学の世界展開力事この方法は、Apache Commons.ご利用の春、支柱や他の多くの一般的使用のjavaライブラリ、もしApache commons含まれます。される前に実行されますのでcommons-lang.jar ファイルです。ここでは方法 NumberUtils い:
isNumber[1]
public static boolean isNumber(java.lang.String str)
Checks whether the String a valid Java number.
Valid numbers include hexadecimal marked with the 0x qualifier, scientific notation and numbers marked with a type qualifier (e.g. 123L).
Null and empty String will return false.
Parameters:
str - the String to check
Returns:
true if the string is a correctly formatted number
他のヒント
<のhref = "http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#matches(java.lang.String)" のrel =」を使用noreferrer nofollowを ">正規表現の
正確な正規表現は、 Double.valueOf(String)
の
無効な文字列でこのメソッドを呼び出すと
NumberFormatException
がスローされることがないようにするには、以下の正規表現が入力文字列をスクリーニングするために使用することができます:final String Digits = "(\\p{Digit}+)"; final String HexDigits = "(\\p{XDigit}+)"; // an exponent is 'e' or 'E' followed by an optionally // signed decimal integer. final String Exp = "[eE][+-]?"+Digits; final String fpRegex = ("[\\x00-\\x20]*"+ // Optional leading "whitespace" "[+-]?(" + // Optional sign character "NaN|" + // "NaN" string "Infinity|" + // "Infinity" string // A decimal floating-point string representing a finite positive // number without a leading sign has at most five basic pieces: // Digits . Digits ExponentPart FloatTypeSuffix // // Since this method allows integer-only strings as input // in addition to strings of floating-point literals, the // two sub-patterns below are simplifications of the grammar // productions from the Java Language Specification, 2nd // edition, section 3.10.2. // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+ // . Digits ExponentPart_opt FloatTypeSuffix_opt "(\\.("+Digits+")("+Exp+")?)|"+ // Hexadecimal strings "((" + // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt "(0[xX]" + HexDigits + "(\\.)?)|" + // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" + ")[pP][+-]?" + Digits + "))" + "[fFdD]?))" + "[\\x00-\\x20]*"); // Optional trailing "whitespace" if (Pattern.matches(fpRegex, myString)) Double.valueOf(myString); // Will not throw NumberFormatException else { // Perform suitable alternative action }
ここでは正常に動作して正規表現ベースのユーティリティ機能(フィットできなかった「読み、それを維持しながら、」正規表現にチェック)されます:
public class TestRegexp {
static final String NUM_REGEX=
"-?((([0-9]{1,3})(,[0-9]{3})*)|[0-9]*)(\\.[0-9]+)?([Ee][0-9]*)?";
public static boolean isNum(String s) {
return s!=null && s.length()>0 && s.matches(NUM_REGEX);
}
public static void main(String[]args) {
String[] values={
"",
"0",
"0.1",
".1",
"-.5E5",
"-12,524.5E5",
"-452,456,456,466.5E5",
"-452,456,456,466E5",
"22,22,2.14123415e1",
};
for (String value : values) {
System.out.println(value+" is a number: "
+isNum(value));
}
}
}
うん、正規表現は、トリックを行う必要があります。私は唯一の.Net正規表現を知っているが、すべての正規表現の言語はかなり似ので、これはあなたが始める必要があります。あなたは、Javaの正規表現クラスでビットのまわりでそれをキックしたい場合がありますので、私はそれをテストしていない。
"-?(([0-9]{1,3}(,[0-9{3,3})*)|[0-9]*)(\.[0-9]+(e-?[0-9]*)?)?"
正規表現の制御構文の一部:
? - オプションの要素
| - OR演算子。それらが正しくフォーマットされた場合は基本的に私は、カンマの有無にかかわらず数字を可能にした。
[] - 使用できる文字のセット
{、} - 要素の最小最大
* - 要素、0〜無限大の任意の数の
+ - 無限大に少なくとも一つの要素、1
\ - エスケープ文字
。 - 任意の文字(したがってそれがエスケープされた理由)