正则表达式匹配所有的数字(所有的实数,整数)

这个让我找了半天,网上好多写的都是不靠谱的,看着很长一串,要么不对,要么有遗漏.让人抓狂.后来还是在StackOverflow上找到了好用的:

"
^-?\\d*(\\.\\d+)?$"

Qt下使用如下:

QRegExp rx("-?\\d*(\\.\\d+)?$");
if(-1 == rx.indexIn(str.trimmed()))
{
      qDebug() << "Not digit ";
}

 

上述不支持千分位的写法.

some strings that matches with this:

894
923.21
76876876
.32
-894
-923.21
-76876876
-.32

some strings that doesn't:

hello
9bye
hello9bye
888,323
5,434.3
-8,336.09
87078.

参考文章:https://stackoverflow.com/questions/273141/regex-for-numbers-only
posted @ 2020-09-21 20:40  心媛意码  阅读(1919)  评论(0编辑  收藏  举报