Qt-判断字符串中是否存在异常\error……等报警字符

注意:前面都是基础讲解,如果有什么不懂的可以看看,但是如果只是追求实际运用场景,建议从辅助理解时间开始看

原文

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差不多。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

世转神风-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值