注意:前面都是基础讲解,如果有什么不懂的可以看看,但是如果只是追求实际运用场景,建议从辅助理解时间开始看
原文
QRegularExpression::QRegularExpression(const QString &pattern, QRegularExpression::PatternOptions options = NoPatternOption)
Constructs a QRegularExpression object using the given pattern as pattern and the options as the pattern options.
它说,使用给定的模式作为模式,以及选项作为模式选项,构造一个QRegularExpression对象。
QRegularExpression::CaseInsensitiveOption 0x0001 The pattern should match against the subject string in a case insensitive way. This option corresponds to the /i modifier in Perl regular expressions.
它说,该模式应以不区分大小写的方式匹配目标字符串。此选项对应Perl正则表达式中的/i修饰符。
QRegularExpressionMatch QRegularExpression::match(const QString &subject, int offset = 0, QRegularExpression::MatchType matchType = NormalMatch, QRegularExpression::MatchOptions matchOptions = NoMatchOption) const
Attempts to match the regular expression against the given subject string, starting at the position offset inside the subject, using a match of type matchType and honoring the given matchOptions.The returned QRegularExpressionMatch object contains the results of the match.See also QRegularExpressionMatch and normal matching.
它说,尝试从主题字符串中的偏移位置开始,使用指定的匹配类型(matchType)并遵循给定的匹配选项(matchOptions),将正则表达式与主题字符串进行匹配。返回的QRegularExpressionMatch对象包含匹配结果。另请参阅QRegularExpressionMatch和常规匹配。
辅助理解时间
如标题所说,就是最好的理解。当然,在这里要是配一点案例代码,会对大家更友好一点。
步骤一 初始化(这个代码做局部也好,做全局也行,作者用过全局,挺好用的)
// 初始化正则表达式模式,用于查找异常关键字
exceptionPattern("\\b(error|exception|fail|warning|异常)\\b", QRegularExpression::CaseInsensitiveOption)
步骤二 封装成功能函数,这样具有兼容性
bool ClientWindow::isExceptionLine(const QString &line) const
{
// 检查行是否包含异常关键字,例如"error"、"exception"等
// exceptionPattern是一个预定义的正则表达式,match()方法用于检查,hasMatch()返回是否匹配
return exceptionPattern.match(line).hasMatch();
}
步骤三 案例使用
bool isException = isExceptionLine(message);
作者在做报警弹框时,会用来作为辅助判断功能,用来判断是否是报警或者仅仅是普通弹框。
这个功能整体其实和QString::contains差不多。
7504

被折叠的 条评论
为什么被折叠?



