当前位置:网站首页>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
 Insert picture description here

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