当前位置:网站首页>[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
边栏推荐
- Spark 之 built-in functions
- 免费的 SSH 和 Telnet 客户端PuTTY
- vscode korofileheader 的配置
- Contents in qlistwidget are not displayed
- 思维的技术:如何破解工作生活中的两难冲突?
- Webrtc series - Nomination and ice of 7-ice supplement for network transmission_ Model
- The form verifies the variables bound to the V-model, and the solution to invalid verification
- MATLAB快速将影像的二维坐标转换为经纬度坐标
- C Primer Plus 第11章_字符串和字符串函数_代码和练习题
- Webrtc Series - Network Transport 7 - ice Supplement nominations and ice Modèle
猜你喜欢
随机推荐
表单校验 v-model 绑定的变量,校验失效的解决方案
QListWidgetItem上附加widget
tracepoint
Win 10 如何打开环境变量窗口
Spark 之 Projection
Multithreading basic part2
EasyExcel:读取Excel数据到List集合中
乐观事务和悲观事务
【Cocos Creator 3.5.1】this. node. Use of getposition (this.\u curpos)
Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer
mysql 查询时将状态改为相对应的文字
【QT小记】QT元对象系统简单认识
【Cocos Creator 3.5.1】input.on的使用
【Cocos Creator 3.5.1】event.getButton()的使用
多线程带来的的风险——线程安全
QListWidget中的内容不显示
C# netcore中 配置帮助类IConfiguration
1317. convert an integer to the sum of two zero free integers
MATLAB快速将影像的二维坐标转换为经纬度坐标
函数式 连续式