当前位置:网站首页>[QT notes] basic use of qregularexpression in QT
[QT notes] basic use of qregularexpression in QT
2022-06-27 06:09:00 【Lin Qi sevenlin】
- QT5 The use of regular expressions in... Is recommended QRegularExpression, No longer use QRegExp
- QRegularExpression in comparison with QRegExp Optimized and improved
The main classes used
- QRegularExpression Create regular expression objects
- QRegularExpressionMatch Get string matching results
- QRegularExpressionMatchIterator Get string match result iterator ( The global matching , Match multiple )
The main function
- verification
- String search
- String find and replace
- String segmentation
Create objects ( structure )
QRegularExpression re("a pattern");
Set regular expression mode
QRegularExpression re;
re.setPattern("another pattern");
Get regular expression pattern
QRegularExpression re("a third pattern");
QString pattern = re.pattern(); // pattern == "a third pattern"
Set mode options
// Case insensitive
QRegularExpression re("Qt rocks", QRegularExpression::CaseInsensitiveOption);
// Multi-line matching
QRegularExpression re("^\\d+$");
re.setPatternOptions(QRegularExpression::MultilineOption);
Get mode options
QRegularExpression re = QRegularExpression("^two.*words$", QRegularExpression::MultilineOption |
QRegularExpression::DotMatchesEverythingOption);
QRegularExpression::PatternOptions options = re.patternOptions();
// options == QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption
string matching ( Match result single )
The capture group in the pattern is from 1 Numbered starting , Implicitly capture group number 0 Used to capture substrings that match the entire pattern .
// Check whether the match is successful
QRegularExpression re("\\d\\d \\w+");
QRegularExpressionMatch match = re.match("abc123 def");
bool hasMatch = match.hasMatch(); // true
// Get string matching results
QRegularExpression re("\\d\\d \\w+");
QRegularExpressionMatch match = re.match("abc123 def");
if (match.hasMatch()) {
QString matched = match.captured(0); // matched == "23 def"
}
// From the offset 1 Start matching string
QRegularExpression re("\\d\\d \\w+");
QRegularExpressionMatch match = re.match("12 abc 45 def", 1);
if (match.hasMatch()) {
QString matched = match.captured(0); // matched == "45 def"
}
// Extract string Use captured(int nth = 0)
QRegularExpression re("^(\\d\\d)/(\\d\\d)/(\\d\\d\\d\\d)$");
QRegularExpressionMatch match = re.match("08/12/1985");
if (match.hasMatch()) {
QString day = match.captured(1); // day == "08"
QString month = match.captured(2); // month == "12"
QString year = match.captured(3); // year == "1985"
}
// Extract string , Use captured(const QString &name)
QRegularExpression re("^(?<date>\\d\\d)/(?<month>\\d\\d)/(?<year>\\d\\d\\d\\d)$");
QRegularExpressionMatch match = re.match("08/12/1985");
if (match.hasMatch()) {
QString date = match.captured("date"); // date == "08"
QString month = match.captured("month"); // month == "12"
QString year = match.captured("year"); // year == 1985
}
Get substring index
QRegularExpression re("abc(\\d+)def");
QRegularExpressionMatch match = re.match("XYZabc123defXYZ");
if (match.hasMatch()) {
// Starting index
int startOffset = match.capturedStart(1); // startOffset == 6
// End index
int endOffset = match.capturedEnd(1); // endOffset == 9
}
The global matching ( Multiple matching results )
QRegularExpression re("(\\w+)");
QRegularExpressionMatchIterator i = re.globalMatch("the quick fox")
QStringList words;
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
QString word = match.captured(1);
words << word;
} // words contains "the", "quick", "fox"
Check whether the regular expression has syntax errors
QRegularExpression invalidRe("(unmatched|parenthesis");
if (!invalidRe.isValid()) {
QString errorString = invalidRe.errorString(); // errorString == "missing )"
int errorOffset = invalidRe.patternErrorOffset(); // errorOffset == 22
}
Partial matching
Apply to
- User input validation
- Incremental multisegment matching
Match type
- QRegularExpression::PartialPreferCompleteMatch
The pattern string partially matches the topic string . If a partial match is found , Record it , And try other matching alternatives as usual . If an exact match is found , Takes precedence over partial matching ; under these circumstances , Only complete matches are reported . contrary , If no exact match is found ( But only a partial match ), Then the report partially matches . - QRegularExpression::PartialPreferFirstMatch
The pattern string partially matches the topic string . If a partial match is found , Then the match stops and a partial match is reported . under these circumstances , Will not try other matching alternatives ( May result in an exact match ). Besides , This matching type assumes that the topic string is just a substring of larger text , also ( In this text ) There are other characters besides the end of the topic string , This can lead to surprising results .
/***************QRegularExpression::PartialPreferCompleteMatch***************/
// Example 1
QString pattern("^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d?, \\d\\d\\d\\d$");
QRegularExpression re(pattern);
QString input("Jan 21,");
QRegularExpressionMatch match = re.match(input, 0, QRegularExpression::PartialPreferCompleteMatch);
// The whole match
bool hasMatch = match.hasMatch(); // false
// Partial matching
bool hasPartialMatch = match.hasPartialMatch(); // true
QString captured = match.captured(0); // captured == "Jan 21,"
// Example 2
QString pattern("^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d?, \\d\\d\\d\\d$");
QRegularExpression re(pattern);
QString input("Dec 8, 1985");
QRegularExpressionMatch match = re.match(input, 0, QRegularExpression::PartialPreferCompleteMatch);
bool hasMatch = match.hasMatch(); // true
bool hasPartialMatch = match.hasPartialMatch(); // false
// Example 3
QRegularExpression re("abc\\w+X|defY");
QRegularExpressionMatch match = re.match("abcdef", 0, QRegularExpression::PartialPreferCompleteMatch);
bool hasMatch = match.hasMatch(); // false
bool hasPartialMatch = match.hasPartialMatch(); // true
QString captured = match.captured(0); // captured == "abcdef"
/***************QRegularExpression::PartialPreferFirstMatch***************/
// Example 1
QRegularExpression re("abc|ab");
QRegularExpressionMatch match = re.match("ab", 0, QRegularExpression::PartialPreferFirstMatch);
bool hasMatch = match.hasMatch(); // false
bool hasPartialMatch = match.hasPartialMatch(); // true
// Example 2
QRegularExpression re("abc(def)?");
QRegularExpressionMatch match = re.match("abc", 0, QRegularExpression::PartialPreferFirstMatch);
bool hasMatch = match.hasMatch(); // false
bool hasPartialMatch = match.hasPartialMatch(); // true
// Example 3
QRegularExpression re("(abc)*");
QRegularExpressionMatch match = re.match("abc", 0, QRegularExpression::PartialPreferFirstMatch);
bool hasMatch = match.hasMatch(); // false
bool hasPartialMatch = match.hasPartialMatch(); // true
Regular expression basic syntax
https://blog.csdn.net/lin786063594/article/details/125301089?spm=1001.2014.3001.5501
Learning materials
QT Help document
边栏推荐
- Multithreading basic part part 1
- 函数式 连续式
- Kubesphere cluster configuration NFS storage solution - favorite
- Senior [Software Test Engineer] learning route and necessary knowledge points
- Unicast, multicast and broadcast of IP network communication
- 多线程基础部分Part 1
- 古典密码体制--代换和置换
- LeetCode 0086.分隔链表
- QListWidgetItem上附加widget
- Small program of C language practice (consolidate and deepen the understanding of knowledge points)
猜你喜欢

Thinking technology: how to solve the dilemma in work and life?

My opinion on test team construction

C语言练手小项目(巩固加深知识点理解)

Quick personal site building guide using WordPress

汇编语言-王爽 第11章 标志寄存器-笔记

卷积神经网络---CNN模型的应用(找矿预测)

多线程基础部分Part3

Program ape learning Tiktok short video production

How to check the frequency of memory and the number of memory slots in CPU-Z?

JVM object composition and storage
随机推荐
693. alternate bit binary number
yaml文件加密
Kubesphere cluster configuration NFS storage solution - favorite
JVM的垃圾回收机制
Nlp-d62-nlp competition d31 & question brushing D15
【合辑】点云基础知识及点云催化剂软件功能介绍
310. minimum height tree
Go日志-Uber开源库zap使用
Redis4.0新特性-主动内存碎片整理
Matlab quickly converts two-dimensional coordinates of images into longitude and latitude coordinates
树莓派4B上运行opcua协议DEMO接入kubeedge
Assembly language - Wang Shuang Chapter 13 int instruction - Notes
426 binary tree (513. find the value in the lower left corner of the tree, 112. sum of paths, 106. construct a binary tree from the middle order and post order traversal sequence, 654. maximum binary
tar: /usr/local:归档中找不到tar: 由于前次错误,将以上次的错误状态退出
Assembly language - Wang Shuang Chapter 3 notes and experiments
Sqlsever 字段相乘后保留2位小数
Contents in qlistwidget are not displayed
Webrtc series - Nomination and ice of 7-ice supplement for network transmission_ Model
创建一个基础WDM驱动,并使用MFC调用驱动
Spark 之 built-in functions