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

 


 

 



 

相关文章:

  • 2021-07-21
  • 2021-10-13
  • 2021-11-17
  • 2022-12-23
  • 2021-07-18
  • 2022-12-23
猜你喜欢
  • 2021-10-03
  • 2022-12-23
  • 2022-12-23
  • 2021-05-25
  • 2021-10-17
  • 2022-12-23
相关资源
相似解决方案