当前位置:网站首页>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
边栏推荐
- Auto. JS custom dialog box
- 【ACM】70. climb stairs
- Generate verification code
- Crack sliding verification code
- STM32 learning record 0008 - GPIO things 1
- Error reported when running tensorboard: valueerror: duplicate plugins for name projector, solution
- Vulnérabilité d'exécution de la commande de fond du panneau de commande JD - freefuck
- Rust: a simple example of TCP server and client
- 硬核解析Promise對象(這七個必會的常用API和七個關鍵問題你都了解嗎?)
- 读取excel,int 数字时间转时间
猜你喜欢
Install pyshp Library
Jenkspy package installation
Multifunctional toolbox wechat applet source code
A few lines of code teach you to crawl lol skin pictures
【ACM】376. Swing sequence
Cygwin64 right click to add menu, and open cygwin64 here
ArcGIS license error -15 solution
JD-FreeFuck 京东薅羊毛控制面板 后台命令执行漏洞
Quantexa CDI(场景决策智能)Syneo平台介绍
Auto. JS custom dialog box
随机推荐
Reptile efficiency improvement method
Closure type of rust (difference between FN, fnmut and fnone)
JD freefuck Jingdong HaoMao control panel background Command Execution Vulnerability
Cygwin64 right click to add menu, and open cygwin64 here
Rust: how to implement a thread pool?
How to restore MySQL database after win10 system is reinstalled (mysql-8.0.26-winx64. Zip)
Excel opens large CSV format data
Batch export ArcGIS attribute table
JD-FreeFuck 京东薅羊毛控制面板 后台命令执行漏洞
C medium? This form of
Solution to Chinese garbled code after reg file is imported into the registry
【ACM】455. Distribute Biscuits (1. Give priority to big biscuits to big appetite; 2. Traverse two arrays with only one for loop (use subscript index -- to traverse another array))
Visualization of residential house prices
NVIDIA Jetson: GStreamer and openmax (GST OMX) plug-ins
Test post and login function
re正则表达式
Error reported when running tensorboard: valueerror: duplicate plugins for name projector, solution
In shell programming, the shell file with relative path is referenced
【ACM】70. climb stairs
Deep learning classic network analysis and target detection (I): r-cnn