
正文
Qt 正则表达式检查 IP 格式
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
KillerSmath 2018年6月29日 下午10:41
@Pranit-Patil Hi there.
Like @jonB says above, you should to replace\.to\\.to avoid an error of escape caracter.
But if you want to validate just if the user insert the complete ip, you must to remove the "optional counter caracter" of each group of numbers.
Example:
"(\\." + IpRange + ")?"
this sentence accepts:
(void) and 0 until 255 number because of ? caracter that means: 0 or 1 time
NOTE: QRegExp is an old class of Qt4, if you are using Qt5+, i suggest you to use QRegularExpression
Below is a code solving the problems:
QString IpRange = "(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])";
QRegularExpression IpRegex ("^" + IpRange
+ "(\\." + IpRange + ")"
+ "(\\." + IpRange + ")"
+ "(\\." + IpRange + ")$");
QRegularExpressionValidator *ipValidator = new QRegularExpressionValidator(IpRegex, this);







