zoukankan      html  css  js  c++  java
  • Qt中使用正则表达式返回匹配的所有结果集

    Python的正则中有findAll函数返回一个所有匹配的结果list.

    今天在使用Qt的时候发现似乎没有类似的方法.进而自己写了一个, 代码如下

    /**
    *@brief获取所有的匹配结果
    *@paramtext要匹配的文本
    **@paramregexp正则表达式串
    *@return匹配的结果集
    */
    QSet<QString>UploadBase::getAllMatchResults(constQStringtext,constQStringregexp)
    {
    QSet<QString>resultSet;
    
    
    QRegExprx(regexp);
    intpos=0;
    
    
    while((pos=rx.indexIn(text,pos))!=-1)
    {
    pos+=rx.matchedLength();
    QStringresult=rx.cap(0);
    resultSet<<result;
    }
    
    
    returnresultSet;
    }
    
    
    测试:
    假如在如下的test.h 要获取所有include的文件名
    //test.h
    #include <Metro.h>
    #include <FreeSixIMU.h>
    #include <FIMU_ADXL345.h>
    #include <FIMU_ITG3200.h>
    #include <Wire.h>
    #include <Wire.h> 
    #include <LiquidCrystal_I2C.h>
    #include <IRremote.h>
    
    
    

    可以这么使用

    QSet<QString>libReference;
    QFilefile(filePath);//filePath是test.h的路径
    if(!file.open(QFile::ReadOnly))
    {
    qDebug()<<file.errorString();
    returnlibReference;//returnaemptyobject
    }
    
    
    QStringcode=file.readAll();
    
    
    libReference=getAllMatchResults(code,"\\w+\\.h");
    
    
    libReference中的结果为 Metro.h, FreeSixIMU.h, FIMU_ADXL345.h, FIMU_ITG3200.h, Wire.h, LiquidCrystal_I2C.h, IRremote.h
    
    
    
    



  • 相关阅读:
    [NOIP-P1125]逃亡的准备
    [NOIP-P1125]两个数差
    问题 B: [NOIP-P1125]飙车
    [NOIP-P1125]计算概率
    牛跳
    化学方程式
    c++第十七章-(内联函数)
    c++第十六章-(函数模板与类模板)
    《将博客搬至CSDN》
    cocoaPods安装与使用
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3015702.html
Copyright © 2011-2022 走看看