检查Java中是否可以将String解析为Double的最快方法 [英] Fastest way to check if a String can be parsed to Double in Java
问题描述
注意:我不想将值转换为Double,我只想知道是否可能。即 private boolean isDouble(String value)
。
它使用与Double类使用相同的正则表达式。这里有很好的记录:
http://docs.oracle.com/javase/6/docs/api/java/lang/Double.html#valueOf%28java.lang.String %29
以下是代码部分:
避免在无效字符串上调用此方法并抛出NumberFormatException,下面的正则表达式可用于屏蔽输入字符串:
final String Digits =(\\p {Digit} +);
final String HexDigits =(\\p {XDigit} +);
//一个指数是'e'或'E',后跟一个可选的
//有符号的十进制整数。
final String Exp =[eE] [+ - ]?+数字;
final String fpRegex =
([\\\x00-\\x20] *+ //可选的前导空格
[+ - ]?(+ //可选符号字符
NaN |+ //NaNstring
Infinity |+ //Infinitystring
//十进制浮点数代表一个有限的正值
//没有前导符号的字符串最多有五个基本部分:
// Digits。Digits ExponentPart FloatTypeSuffix
//
//由于此方法允许整数唯一的字符串作为输入
//除了浮点文字的字符串之外,
//下面的两个子模式是简化语法
//从Java语言规范,第二
//版,第3.10.2节。
// Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
((+ Digits +(\\。)?(+ Digits +?)(+ Exp +)?)|+
//。数字ExponentPart_opt FloatTypeSuffix_opt
(\\。(+ Digits +)(+ Exp +)?)|+
//十六进制字符串
( +
// 0 [xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
(0 [xX]+ HexDigits +(\\。)?)|+
// 0 [xX] HexDigits_opt。HexDigits BinaryExponent FloatTypeSuffix_opt
(0 [xX]+ HexDigits +?(\\。)+ HexDigits +)+
$ b[+] $$$$$)+
x20] *); //可选尾随的空格
if(Pattern.matches(fpRegex,myString))
Double.valueOf(myString); //不会抛出NumberFormatException
else {
//执行适当的替代动作
}
I know there's a million ways of doing this but what is the fastest? This should include scientific notation.
NOTE: I'm not interested in converting the value to Double, i'm only interested in knowing if it's possible. i.e. private boolean isDouble(String value)
.
You can check it using the same regular expression the Double class uses. It's well documented here:
http://docs.oracle.com/javase/6/docs/api/java/lang/Double.html#valueOf%28java.lang.String%29
Here is the code part:
To avoid calling this method on an invalid string and having a NumberFormatException be thrown, the regular expression below can be used to screen the input string:
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
}
这篇关于检查Java中是否可以将String解析为Double的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!