Qt4 从QRegex模式获取字符串(以字符串列表/数组的形式返回)
例如:Qt4 从QRegex模式获取字符串(以字符串列表/数组的形式返回),qt4,qregexp,Qt4,Qregexp,例如: QFile controller_selected_file(loaded_file); if (controller_selected_file.open(QIODevice::ReadOnly)) { // grab a data QTextStream in(&controller_selected_file); // read all QString read_data = in.readAll(); // Regex for
QFile controller_selected_file(loaded_file);
if (controller_selected_file.open(QIODevice::ReadOnly))
{
// grab a data
QTextStream in(&controller_selected_file);
// read all
QString read_data = in.readAll();
// Regex for match function "public function something()"
QRegExp reg("(static|public|final)(.*)function(.*)[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]", Qt::CaseInsensitive);
// Read by regex and file
reg.indexIn(read_data);
QStringList data_render = reg.capturedTexts();
qDebug() << data_render;
qDebug() << data_render[0];
qDebug() << data_render[1];
qDebug() << data_render[3];
// ...
}
编辑:我想抓取所有出现在PHP文件字符串。当您接收文件中的所有字符串而不是用正则表达式定义的字符串时,会出现问题
谢谢你的尊重 如果我理解您的问题,我认为您需要将正则表达式修改为
QRegExp reg(((static | public | final)。*function.*\([\\w\\s,]*\)代码>
使用上面的RegExp,您可以匹配类似于静态函数newFunc(int gen_x,float A_b_c)
然后输出将是
19
"static function newFunc( int generic, float special )"
我希望这就是您想要的。警告:未知转义序列:'\')”[默认情况下启用]
在QRegExp reg(((static | public | final)。*函数。*\([\\w\\s,]*\)代码>将\(
和\)
更改为\(
和\)
尝试在文件中添加另一个函数,它将捕获所有,而不仅仅是捕获的字符串:(
QFile controller_selected_file(loaded_file);
if (controller_selected_file.open(QIODevice::ReadOnly)) {
// grab a data
QTextStream in(&controller_selected_file);
// read all
QString read_data = in.readAll();
// Regex for match function "public function something()"
QRegExp reg( "((static|public|final).*function.*\([\\w\\s,]*\))" );
// Read by regex and file
qDebug() << reg.indexIn( read_data );
qDebug() << reg.cap( 1 );
// ...
}
"This is some text: static function newFunc( int generic, float special )
And it also contains some other text, but that is not important"
19
"static function newFunc( int generic, float special )"