如何编写将一段复杂字符串中每个冒号后的数字替换为100倍的该数字,保持其他内容不变。
import re
# 将匹配的数字乘以 10
def double(matched):
value = matched.group('value')
return ':'+str(float(value[1:]) * 10)
s = 'A:2.3G:4.00HFD:5.67'
print(re.sub('(?P<value>\:\d+(\.\d+)?)', double, s))
在冒号后面的数字尾部插入两个0不就可以了?
冒号后面的数字?水很深!
123
12.3
-12
.12
-1.2e-3
inf
-inf
nan
nan(snan)
nan(ind)
……
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/format-specification-syntax-printf-and-wprintf-functions?view=vs-2019
// https://docs.microsoft.com/zh-cn/cpp/porting/visual-cpp-change-history-2003-2015?view=vs-2019
//
// Starting in Visual Studio 2015, if the argument that corresponds to a floating-point conversion specifier (a, A, e, E, f, F, g, G) is infinite,
// indefinite, or NaN, the formatted output conforms to the C99 standard. This table lists the formatted output:
// Value Output
// -------------- ---------
// infinity inf
// Quiet NaN nan
// Signaling NaN nan(snan)
// Indefinite NaN nan(ind)
// Any of these values may be prefixed by a sign. If a floating-point type conversion specifier character is a capital letter,
// then the output is also formatted in capital letters. For example, if the format specifier is %F instead of %f, an infinity
// is formatted as INF instead of inf. The scanf functions can also parse these strings, so these values can make a round trip
// through printf and scanf functions.
//
// Before Visual Studio 2015, the CRT used a different, non-standard format for output of infinite, indefinite, and NaN values:
// Value Output
// ------------------------------ -------------------------
// + infinity 1.#INF random-digits
// - infinity -1.#INF random-digits
// Indefinite (same as quiet NaN) digit .#IND random-digits
// NaN digit .#NAN random-digits
// Any of these may have been prefixed by a sign, and may have been formatted slightly differently depending on field width and precision,
// sometimes with unusual effects. For example, printf("%.2f\n", INFINITY) would print 1.#J because the #INF would be "rounded" to 2 digits of precision.