QT开发中文网

QRegExp 正则表达式使用

QRegExp是Qt框架提供的正则表达式库,可以用来处理字符串匹配、替换等任务。下面是一些基本的使用方法:

  1. 创建一个QRegExp对象
QRegExp regExp("pattern");
  1. 匹配字符串
QString text = "This is a test";
if (regExp.exactMatch(text)) {
    // 匹配成功
}
  1. 替换字符串
QString text = "This is a test";
QString replacement = "another";
text.replace(regExp, replacement);
  1. 捕获子串
QString text = "The price is $10.99";
QRegExp regExp("\\$(\\d+\\.\\d+)");
if (regExp.indexIn(text) != -1) {
    QString price = regExp.cap(1);  // 获取第一个捕获子串
}
  1. 使用捕获组和反向引用
QString text = "John Smith";
QRegExp regExp("(\\w+) (\\w+)");
QString formatted = text.replace(regExp, "\\2, \\1");  // 反向引用
// 输出:Smith, John

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=1269

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?