当前位置:网站首页>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
边栏推荐
- Interpretation and compilation of JVM
- What are the relationships and differences between threads and processes
- re正則錶達式
- Test post and login function
- C medium? This form of
- How to install jsonpath package
- 【ACM】70. 爬楼梯
- Scikit learn sklearn 0.18 official document Chinese version
- Feign requests the log to be printed uniformly
- positioner
猜你喜欢
MATLAB从入门到精通(二)
解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
Calculation of fishing net road density
Install pyshp Library
由tcl脚本生成板子对应的vivado工程
【ACM】376. Swing sequence
Process management command
【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))
How to install jsonpath package
How to restore MySQL database after win10 system is reinstalled (mysql-8.0.26-winx64. Zip)
随机推荐
Generate verification code
C medium? This form of
Auto. JS custom dialog box
Multifunctional toolbox wechat applet source code
Resolves the interface method that allows annotation requests to be written in postman
Array rotation
Robocode tutorial 7 - Radar locking
WIN1 remote "this may be due to credssp encryption Oracle correction" solution
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (1)
Gobang game based on pyGame Library
Read excel, int digital time to time
How to restore MySQL database after win10 system is reinstalled (mysql-8.0.26-winx64. Zip)
STM32学习记录0008——GPIO那些事1
[UDS unified diagnostic service] IV. typical diagnostic service (4) - online programming function unit (0x34-0x38)
What are the relationships and differences between threads and processes
【ACM】455. 分发饼干(1. 大饼干优先喂给大胃口;2. 遍历两个数组可以只用一个for循环(用下标索引--来遍历另一个数组))
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (2)
Robocode tutorial 8 - advanced robot
Solving the problem of displaying too many unique values in ArcGIS partition statistics failed
Refcell in rust