輸入可見字符(除去空格 回車 ...)
QRegExp rxp("^\\w+\\S+$");
IP正則表達式
QRegExp rxp("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");
端口號正則表達式
QRegExp rxp("^([0-9]|[1-9]\\d|[1-9]\\d{2}|[1-9]\\d{3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])$");
0-100的保留兩位小數正則表達式
QRegExp rxp("^(100|0?[0-9]?\\d(\\.\\d{1,2})?)$");
只能輸入數字
QRegExp rxp("^[0-9]*$");
應用實例:
1 QRegExp rxp("^\\w+\\S+$"); //輸入可見字符(除去空格 回車 ...) 2 QRegExpValidator *pRep = new QRegExpValidator(rxp, this); 3 ui->lineEdit_ProjectName->setValidator(pRep);
匹配非負整數(正整數 + 0)
^\d+$
匹配正整數
^[0-9]*[1-9][0-9]*$
匹配非正整數(負整數 + 0)
^((-\d+)|(0+))$
匹配負整數
^-[0-9]*[1-9][0-9]*$
匹配整數
^-?\d+$
匹配非負浮點數(正浮點數 + 0)
^\d+(\.\d+)?$
匹配正浮點數
^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
匹配非正浮點數(負浮點數 + 0)
^((-\d+(\.\d+)?)|(0+(\.0+)?))$
匹配負浮點數
^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$
匹配浮點數
^(-?\d+)(\.\d+)?$
匹配由26個英文字母組成的字符串
^[A-Za-z]+$
匹配由26個英文字母的大寫組成的字符串
^[A-Z]+$
匹配由26個英文字母的小寫組成的字符串
^[a-z]+$
匹配由數字和26個英文字母組成的字符串
^[A-Za-z0-9]+$
匹配由數字、26個英文字母或者下划線組成的字符串
^\w+$
匹配email地址
^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$
匹配url
^[a-zA-z]+://匹配(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$
匹配html tag
<\s*(\S+)(\s[^>]*)?>(.*?)<\s*\/\1\s*>