這個讓我找了半天,網上好多寫的都是不靠譜的,看着很長一串,要么不對,要么有遺漏.讓人抓狂.后來還是在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