[QT] Regular matching and QRegExp

1. Basic usage of regular expressions

character describe
[ ] Matches all characters in [ ]. Such as [123] is to match 1 or 2 or 3, match "2569431012" is to match all 1 or 2 or 3 numbers, that is, 2, 3, 1, 1, 2, not to match consecutive 123.
[^ ] Matches all characters except [ ]. Such as [^ABC], means to match all characters except ABC.
[0-9] represents an interval.
\d Matches a numeric character. Equivalent to [0-9].
\D Matches a non-numeric character. Equivalent to [^0-9].
. Match any single character except newline (\n, \r). To match ".", such as IP address matching, you must use the escape character "\.".
\w Match letters, numbers, underscores. Equivalent to [A-Za-z0-9].
^ Matches the beginning of the input string. If used in a square bracket expression, such as [^ABC], it means to match all characters except ABC. To match the ^ character itself, use the escape character "\^".
$ Matches the end of the input string. $ also matches '\n' or '\r' if the Multiline property of the QRegExp object is set. To match the $ character itself, use the escape character "\$".
+ Matches the preceding character or expression one or more times, equivalent to {1,}.
* Matches the preceding character or expression zero or more times, equivalent to {0,}. Such as [0-9]* means any number of numbers
? Matches the preceding character or expression zero or one time, equivalent to {0, 1}.
{n} n is a non-negative number, which means match the preceding character or expression n times.
{n,} Indicates to match the previous character or expression at least n times
{n,m} Both n and m are non-negative numbers, which means that the previous character or expression is matched at least n times and at most m times.
\b Matches a word boundary, the position between a word and a space
\B Non-word boundary matching

Notice:C++中"\"在字符串中表示要用"\\"

2. Commonly used regular expressions

describe expression
number ^[0-9]*$
n-digit number ^\d{n}$
at least n digits ^\d{n,}$
mn digits ^\d{m,n}$
Numbers beginning with zero and non-zero ^(0|[1-9][0-9]*)$
Class D IP: 224.0.0.0~239.255.255.255 ^(22[4-9]|23[0-9])(\.((\d)|([1-9]\d)|(1\d{2})|(2[0-4]\d)|(25[0-5]))){3}$
IP ((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))).){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))

3. Regular expressions for IP addresses

IPv4 address format, the total length is 32 bits = 4 segments * 8 bits, each segment is separated by ., and each segment is a decimal value between 0-255.
Representing 0-255 with regular expressions can be divided into several pieces to consider separately:

Value range features Regular writing
0-9 One digit, only one digit, the value is 0~9 \d
10-99 Two digits, the tens digit is 1-9, and the ones digit is 0~9 [1-9]\d
100-199 Three digits, the highest digit is 1, the tens digit is 0-9, and the ones digit is 0-9 1\d{2}
200-249 Three digits, the highest digit is 2, the tens digit is 0-4, and the ones digit is 0-9 2[0-4]\d
250-255 Three digits, the highest digit is 2, the tens digit is 5, and the ones digit is 0-5 25[0-5]

So the regular notation for an IP address is as follows:

((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))).){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))

4. Basic usage of QRegExp

Example: Restrict input to numbers only for a LineEdit

QRegExp regExp("\\d*");
QValidator* pValidator = new QRegExpValidator(regExp, this);
ui->lineEdit->setValidator(pValidator);

For example: regular match D class IP: 224.0.0.0~239.255.255.255

QRegExp regExp("^(22[4-9]|23[0-9])(\\.((\\d)|([1-9]\\d)|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))){3}$");
ui->lineEdit->setValidator(new QRegExpValidator(regExp, this));

Notice:C++中"\"在字符串中表示要用"\\"

Guess you like

Origin blog.csdn.net/WL0616/article/details/129028042
Recommended