Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
直到现在我还没有使用过正则表达式,但是我需要一个正则表达式来匹配表示正或负数值的字符串,例如
234
-8
3.346
-564.4
它不应该匹配任何文本或混合字符(<>#?_...)、数字和文本,例如
美国广播公司
.-.
<11.45
金额111.43
345.654.33
这应该这样做:
^-?\d+(\.\d+)?$
^字符串的开头
^
-?减号一次或零次
-?
\d+数字,一个或多个
\d+
(\.\d+)?一个点后跟一个或多个数字,整个块一或零次
(\.\d+)?
$字符串结尾
$
还请注意 Utkanos 对您下一个关于 SO 的问题的评论 :)