当前位置:网站首页>Use of regular expressions in QT
Use of regular expressions in QT
2022-04-23 18:15:00 【Talent、me】
QRegExp class
# The header file #include
#.pro Add... To the file QT += core
There are several common function interfaces

How to use function interfaces
Rules in regular expressions
regexp By expression 、 Quantifiers and assertions Built .
Anchor assertion :^( The starting point ) and $( The end point ), These two symbols are not used to match the characters of the string , Instead, it is used to match the position in the string .
int indexIn(const QString &str, int offset = 0, QRegExp::CaretMode caretMode = CaretAtZero) const
effect : distinguish str Whether it matches the expression
offset: from str Subscripts in strings offset Start identifying ,offset The default is 0
Return value : The starting position of the recognition , If not identified as -1
bool exactMatch(const QString &str) const
effect : Equate to indexln()
QString cap(int nth = 0) const
effect : Returns the captured text string
QStringList QRegExp::capturedTexts() const
effect : Returns a list of captured text strings . The first string in the list is the entire matching string . Each subsequent list element contains a string , The string matches regexp One of the ( The captured ) subexpression .
int captureCount() const
effect : Number of recognition characters
bool isValid() const
effect : Judge whether the regular expression is reasonable
int matchedLength() const
effect : The length and size of the recognized character
Substitution of common symbols
| Symbol | effect | Example |
|---|---|---|
| {x,y} | Represents the number of occurrences ,x Minimum number of times ,y Is the maximum number of times | [0-9]{1,2} |
| ? | Represents matching the previous subexpression 0 Time or 1 Time , Equivalent to {0,1} | [0-9][0-9]? |
| * | Represents zero or more times that the preceding subexpression is matched , Equivalent to {0,} | hello(world)* |
| + | Represents matching the previous subexpression one or more times , Equivalent to {1,} | hello(world)+ |
| - | Represents the range of characters | [a-z] |
| ^ | If ^ stay [] Inside , Then the subexpression performs inverse operation | [^abc] |
| | | perhaps | (hello|bye) |
| \b | Assertions are used only for judgment and do not match any characters |
| character | Abbreviation |
|---|---|
| [0-9] | \d |
| [^0-9] | \D |
| All blank symbols | \s |
| All non whitespace characters are good | \S |
| Match a line break | \n |
| Match a tab | \t |
| character 、 Numbers 、 Underline | \w |
| The character 、 Numbers 、 Underline | \W |
Project practice
Learn regular expressions this time , In order to create multiple text boxes for the project, you need to enter text in a certain format , So it's better to use regular expressions to judge .
1、 For example, judge whether the color format entered in the text box is correct (0xFFFFFF)
The regular expression is written as :^(0x|0X)[0-9A-Fa-f]{6,6}$
QString content = ui->lineEdit->text();
QRegExp rx("^(0x|0X)[0-9A-Fa-f]{6,6}$");
if (rx.indexIn(content) == -1) {
QMessageBox::warning(this,"",tr(" The color format is :0xFFFFFF"));
return;
}
2、 For example, judge the code number entered in the text box (yue12345678). Conditions : No less than two digits and no spaces 、 comma 、 Underline and other symbols , And the end can only be at least two digits
Xiaobai really can't think of a regular expression to meet the above conditions , Only two expressions can be used to judge whether the end is two numbers .
QRegExp rxLen("\\d{2,15}$");
QRegExp rxchinese("^[1-9A-Za-z][A-Za-z0-9]*[0-9]{1,15}$");
if (rxLen.indexIn(str) != -1 && rxchinese.indexIn(str) != -1) {
QString numStr = rxLen.cap();
if (QString("%1").arg(numStr.toInt() + ui->sampleCountEdit->text().toInt()).size() != numStr.size()) {
QMessageBox::warning(this,"",tr(" Wrong number format , The number cannot have spaces , Underline , Chinese and other characters , Such as yue123456"));
return;
}
}
3、 Next, write some common requirements expressions , such as
| demand | expression |
|---|---|
| Only numbers can be entered in the input box | ^\d$ |
| The input box can only enter characters | ^[A-Za-z]+$ |
| The input box can only be numeric and 26 A string of English letters | ^[0-9A-Za-z]+$ |
| The input box can only input Chinese | ^[\u4e00-\u9fa5]{0,}$ |
| Enter an integer or a value with at most two decimal places in the input box | ^[0-9]+(.[0-9]{0,2})?$ |
| Enter a negative number or an integer in the input box 、 Values with up to two decimal places | ^(-)?[0-9]+(.[0-9]{0,2})?$ |
| Enter the date format in the input box | ^[1-9]{1}[0-9]{3}-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2][1-9]|3[0-1])$ |
版权声明
本文为[Talent、me]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210610471415.html
边栏推荐
- QT reading and writing XML files (including source code + comments)
- STM32 learning record 0008 - GPIO things 1
- [UDS unified diagnostic service] IV. typical diagnostic service (6) - input / output control unit (0x2F)
- Robocode Tutorial 4 - robocode's game physics
- How to restore MySQL database after win10 system is reinstalled (mysql-8.0.26-winx64. Zip)
- Differences between SSD hard disk SATA interface and m.2 interface (detailed summary)
- Svn simple operation command
- Cygwin64 right click to add menu, and open cygwin64 here
- Pyppeter crawler
- Crawl the product data of Xiaomi Youpin app
猜你喜欢
![[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (2)](/img/91/3272d5ad04cd1d8476c739546f4356.png)
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (2)

Solving the problem of displaying too many unique values in ArcGIS partition statistics failed

Differences between SSD hard disk SATA interface and m.2 interface (detailed summary)

logstash 7. There is a time problem in X. the difference between @ timestamp and local time is 8 hours

【ACM】376. Swing sequence

MATLAB小技巧(6)七种滤波方法比较

Cygwin64 right click to add menu, and open cygwin64 here

Robocode tutorial 5 - enemy class

C network related operations

C medium? This form of
随机推荐
Solving the problem of displaying too many unique values in ArcGIS partition statistics failed
Interpretation and compilation of JVM
MySQL_ 01_ Simple data retrieval
Docker 安装 MySQL
Differences between SSD hard disk SATA interface and m.2 interface (detailed summary)
In shell programming, the shell file with relative path is referenced
【ACM】70. 爬楼梯
【ACM】376. Swing sequence
Rewrite four functions such as StrCmp in C language
【ACM】376. 摆动序列
Multi thread safe reference arc of rust
The difference between deep copy and shallow copy
[UDS unified diagnostic service] IV. typical diagnostic service (6) - input / output control unit (0x2F)
Solution to Chinese garbled code after reg file is imported into the registry
Test post and login function
Multi thread crawling Marco Polo network supplier data
How to restore MySQL database after win10 system is reinstalled (mysql-8.0.26-winx64. Zip)
Using transmittablethreadlocal to realize parameter cross thread transmission
Pyppeter crawler
What are the relationships and differences between threads and processes