我有一个正则表达式"^[0-9]+\.?[0-9]*$")
,用于在Visual C++中匹配双精度数或整数,但它似乎不能工作。 有任何想法吗?
这是我应用代码的方式:
if (System::Text::RegularExpressions::Regex::IsMatch(e0, "^[0-9]+\.?[0-9]*$")){
e0_val = System::Convert::ToDouble(e0);
}
我有一个正则表达式"^[0-9]+\.?[0-9]*$")
,用于在Visual C++中匹配双精度数或整数,但它似乎不能工作。 有任何想法吗?
这是我应用代码的方式:
if (System::Text::RegularExpressions::Regex::IsMatch(e0, "^[0-9]+\.?[0-9]*$")){
e0_val = System::Convert::ToDouble(e0);
}
上述正则表达式并不完美,因为它接受了"09"这样的无效数字。更好的表达式应该是:
"^(-?)(0|([1-9][0-9]*))(\\.[0-9]+)?$"
其中:
1. is an optional negative sign;
2. is zero or a valid non-zero integer;
4. is the optional fracture part;
在理论上,断裂部分应该被写成"(\.[0-9]*[1-9])?",因为一个数不能有尾随的零。实际上,源字符串可能已经被创建为一个固定位数的数字,例如:printf("%.1f", x);
因此,它可能会以零字符轻松结束。当然,这些都是固定点表示法,而不是双精度本身。双精度数也可以写成-1.23e-4,而不是-0.000123。
这个正则表达式本身没有问题,问题出在你的转义上。你需要将 \
字符进行双重转义,因为它也是 C++ 字符串转义字符。
另外还有一种情况,这个正则表达式会认为 1.
是一个有效的浮点数。所以你最好使用 /^[0-9]+(\\.[0-9]+)?$/
,可以避免这种情况的发生。
std::regex rx(R"(^([+-]?(?:[[:d:]]+\.?|[[:d:]]*\.[[:d:]]+))(?:[Ee][+-]?[[:d:]]+)?$)");
匹配字符串:
"1", "0", "10",
"1000.1", "+1",
"+10", "-10", "1.",
".1", "1.1", "+1.",
"-1.", "+.1", "-.1",
"009", "+009", "-009",
"-01e0", "+01E0", "+1e-1",
"+1e+1", "+1.e1", "1E1",
"1E+1", "0.001e-12", "0.111111111111111"
并且不匹配下一个字符串:
".", "1a", "++1",
"+-1", "+", "-.",
"-", "--1.", "1.e.1",
"1e.1", "0+.e0"
这些值看起来像是C++中double
类型的有效值,例如double test = +009.e+10
是可以的。
在ideone.com上尝试运行:https://ideone.com/ooF8sG
/^[0-9]+.[0-9]+$ :使用此正则表达式来匹配双精度数。
可以接受123.123这样的数字类型。
\.
作为小数点,否则会接受任何小数分隔符。 - 0x26res/^[0-9]*[.]?[0-9]+$/
上述正则表达式适用于像“45.5”,“12”,“.12”这样的双精度数。
(\d+)?\.(\d+)?
上面的正则表达式适用于双精度浮点数,例如"45.5"、"12."和".12"
constexpr char double_regex_s[53] =
"^" // accept only if it matches the beginning of the string
"(-?)" // accept a single optional negation
"(0|([1-9][0-9]*))" // number is either zero or some integer that does not start with zero
"(" // begin optional decimals
"\\." // require a dot
"[0-9]+" // any digit is fine, but at least one (std::atof does not require but chrome requires digits after dot)
")?" // end optional decimals
"(" // begin optional scientific exponent
"[eE]" // require an e or E
"[-+]?" // accept optional plus or minus
"[0-9]+" // any digit is fine (tested in chrome JSON.parse(1E000003) works)
")?" // end optional scientific exponent
"$" // accept only if it matches up to the end of the string
;