Получить строки из шаблона QRegex (возврат в виде списка строк/массивов)

Пример:

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];

    // ...
}

Я хочу захватить в файл все, где появляется public function somefunction(), и еще одно public function something($a,$b = "example"), где появляется в файле, но я получаю только полную строку файла или получаю только public в первом массиве.

Итак, я хочу захватить все данные, которые отображаются в виде массивов:

public function somefunction().

Таким образом, просто проанализируйте все имена функций в файле.

Полная функция регулярного выражения в выражении QRegexp:

(static|public|final)(.*)function(.*)[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]

Изменить: я хочу, чтобы все отображалось в строках в файле PHP. Проблемы возникают, когда вы получаете все строки в файле вместо строк, определенных с помощью регулярного выражения.

Спасибо за уважение!


person Marin Sagovac    schedule 25.03.2013    source источник


Ответы (1)


Если я правильно понял ваш вопрос, я думаю, вам нужно изменить регулярное выражение как QRegExp reg( "((static|public|final).*function.*\([\\w\\s,]*\))" );

С приведенным выше RegExp вы можете сопоставить что-то вроде static function newFunc( int gen_x, float A_b_c )

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 );
    // ...
}

Скажем, read_data содержит следующий текст

"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 )"

Я надеюсь, это то, что вы хотите.

person Marcus    schedule 28.03.2013
comment
Замените \( и \) на \\( и \\) - person Marcus; 28.03.2013
comment
Попробуйте добавить еще одну функцию в файл, она захватит все, а не только захваченные строки :( - person Marin Sagovac; 28.03.2013