Qt 正则表达式检查 IP 格式

KillerSmath

 

@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);

 

posted on 2019-11-21 16:38  liujx2019  阅读(1289)  评论(0编辑  收藏  举报

导航