正則表達式匹配所有的數字(所有的實數,整數)


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM