Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 不带查找功能的正则表达式_Qt_Qregexp - Fatal编程技术网

Qt 不带查找功能的正则表达式

Qt 不带查找功能的正则表达式,qt,qregexp,Qt,Qregexp,我正在尝试编写一个正则表达式来查找所有的“;”后面没有新行(\n)字符的字符 ;(?!\\\n) 以及所有不以“;”开头的新行(\n)字符人物: (?< !;)\\\n (?

我正在尝试编写一个正则表达式来查找所有的“;”后面没有新行(\n)字符的字符

;(?!\\\n)
以及所有不以“;”开头的新行(\n)字符人物:

(?< !;)\\\n
(?<!;)\\\n

不幸的是,我使用的是Qt4.7.4QREGEXP,它不支持“向后看”。如何重写上面的正则表达式,使其不使用“向后看”?

引用文档:

零宽度正和零宽度负前瞻断言(?=pattern)和(?!pattern)都受Perl相同语法的支持

可能发生的情况是,您运行的Windows计算机已插入
\r\n
,而不仅仅是
\n
。。。或者可能是在windows计算机上创建的文本文件

我发现lookbehinds需要注意的一点是,对于大多数正则表达式处理程序,不能使用可变长度的lookbehind

如果lookbehinds/lookaheads仍然给您带来麻烦,那么另一种方法是使用捕获组,然后只参考您感兴趣的捕获组

从文档的列表中可以看出:

str = "Nokia Corporation\tqt.nokia.com\tNorway";
QString company, web, country;
rx.setPattern("^([^\t]+)\t([^\t]+)\t([^\t]+)$");
if (rx.indexIn(str) != -1) {
    company = rx.cap(1);
    web = rx.cap(2);
    country = rx.cap(3);
}
捕获组是用括号定义的,稍后可通过从1开始的索引进行访问。第0个索引是整个匹配(不分为捕获组)

希望有帮助。正则表达式在正常工作时会非常有趣。祝你好运

我也喜欢用这个。格式可能与QRegEx稍有不同,但一旦有了它,就可以很快地进行翻译和测试

更新: 这是一个完整的套件,展示了4种不同的捕获字符串以及它们在QRegEx中的发现:

#include <QCoreApplication>
#include <QRegExp>
#include <QString>
#include <QDebug>
#include <QStringList>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QString str =
            "This is a long string;\n"
            "with some semi colons;\n"
            "sometimes followed by a new line;\n"
            "and other times followed; by something else.\n"

            "(;)([^\\n]) find a semicolon and a new line\n"
            "(;)(?!\\n)  find a semicolon not followed by a new line, negative look-ahead\n"

            "([^;])(\\n) find a non semicolon and a new line\n"
            "(?<!;)(\\n) find a new line, not preceeded by a semicolon.\n";

    QList <QRegExp> rx_list;

    QRegExp rx_colon_and_non_newline;
    rx_colon_and_non_newline.setPattern("(;)([^\\n])");

    QRegExp rx_colon_and_neg_lookahead;
    rx_colon_and_neg_lookahead.setPattern("(;)(?!\\n)");

    QRegExp rx_non_colon_and_newline;
    rx_non_colon_and_newline.setPattern("([^;])(\\n)");

    QRegExp rx_neg_lookbehind_and_newline;
    rx_neg_lookbehind_and_newline.setPattern("(?<!;)(\\n)");

    rx_list << rx_colon_and_non_newline
            << rx_colon_and_neg_lookahead
            << rx_non_colon_and_newline
            << rx_neg_lookbehind_and_newline;

    foreach(QRegExp rx, rx_list)
    {
        int count = 0;
        int pos = 0;
        qDebug() << "Pattern" << rx.pattern();
        while ((pos = rx.indexIn(str, pos)) != -1) {
            QStringList capturedTexts(rx.capturedTexts());

            for(int i = 0; i<capturedTexts.size(); i++)
                capturedTexts[i].replace('\n',"\\n");

            qDebug() << "\t" << count << "Found at position" << pos << capturedTexts;
            // qDebug() << rx.cap();
            pos += rx.matchedLength();
            ++count;
        }
        if(count == 0)
            qDebug() << "\tNo matches found.";
    }


    return a.exec();
}
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
qcorea应用程序(argc、argv);
QString str=
“这是一个长字符串;\n”
“使用一些分号;\n”
“有时后跟新行;\n”
“然后是其他时间;然后是其他时间。\n”
“(;)([^\\n])查找分号和新行\n”
(;)(?!\\n)查找一个分号,分号后面不跟新行,负向前看\n
“([^;])(\\n)查找非分号和新行\n”

“(?引用文件:

零宽度正和零宽度负前瞻断言(?=pattern)和(?!pattern)都受Perl相同语法的支持

可能发生的情况是,您运行的Windows计算机插入了
\r\n
,而不仅仅是
\n
…或者可能是在Windows计算机上创建的文本文件

我发现lookbehinds需要注意的一点是,对于大多数正则表达式处理程序,不能使用可变长度的lookbehind

如果lookbehinds/lookaheads仍然给您带来麻烦,那么另一种方法是使用捕获组,然后只参考您感兴趣的捕获组

从文档的列表中可以看出:

str = "Nokia Corporation\tqt.nokia.com\tNorway";
QString company, web, country;
rx.setPattern("^([^\t]+)\t([^\t]+)\t([^\t]+)$");
if (rx.indexIn(str) != -1) {
    company = rx.cap(1);
    web = rx.cap(2);
    country = rx.cap(3);
}
捕获组是用括号定义的,稍后可通过从1开始的索引进行访问。第0个索引是整个匹配项(不分为捕获组)

希望能有帮助。正则表达式在正常工作时会很有趣。祝你好运

我也喜欢使用它。格式可能与QRegEx有点不同,但一旦你有了它,翻译和测试就相当快了

更新: 这是一个完整的套件,展示了4种不同的捕获字符串以及它们在QRegEx中的发现:

#include <QCoreApplication>
#include <QRegExp>
#include <QString>
#include <QDebug>
#include <QStringList>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QString str =
            "This is a long string;\n"
            "with some semi colons;\n"
            "sometimes followed by a new line;\n"
            "and other times followed; by something else.\n"

            "(;)([^\\n]) find a semicolon and a new line\n"
            "(;)(?!\\n)  find a semicolon not followed by a new line, negative look-ahead\n"

            "([^;])(\\n) find a non semicolon and a new line\n"
            "(?<!;)(\\n) find a new line, not preceeded by a semicolon.\n";

    QList <QRegExp> rx_list;

    QRegExp rx_colon_and_non_newline;
    rx_colon_and_non_newline.setPattern("(;)([^\\n])");

    QRegExp rx_colon_and_neg_lookahead;
    rx_colon_and_neg_lookahead.setPattern("(;)(?!\\n)");

    QRegExp rx_non_colon_and_newline;
    rx_non_colon_and_newline.setPattern("([^;])(\\n)");

    QRegExp rx_neg_lookbehind_and_newline;
    rx_neg_lookbehind_and_newline.setPattern("(?<!;)(\\n)");

    rx_list << rx_colon_and_non_newline
            << rx_colon_and_neg_lookahead
            << rx_non_colon_and_newline
            << rx_neg_lookbehind_and_newline;

    foreach(QRegExp rx, rx_list)
    {
        int count = 0;
        int pos = 0;
        qDebug() << "Pattern" << rx.pattern();
        while ((pos = rx.indexIn(str, pos)) != -1) {
            QStringList capturedTexts(rx.capturedTexts());

            for(int i = 0; i<capturedTexts.size(); i++)
                capturedTexts[i].replace('\n',"\\n");

            qDebug() << "\t" << count << "Found at position" << pos << capturedTexts;
            // qDebug() << rx.cap();
            pos += rx.matchedLength();
            ++count;
        }
        if(count == 0)
            qDebug() << "\tNo matches found.";
    }


    return a.exec();
}
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
qcorea应用程序(argc、argv);
QString str=
“这是一个长字符串;\n”
“使用一些分号;\n”
“有时后跟新行;\n”
“然后是其他时间;然后是其他时间。\n”
“(;)([^\\n])查找分号和新行\n”
(;)(?!\\n)查找一个分号,分号后面不跟新行,负向前看\n
“([^;])(\\n)查找非分号和新行\n”
"(?
Perl的lookbehind断言、“独立”子表达式和条件表达式不受支持

那么
(?
Perl的lookbehind断言、“独立”子表达式和条件表达式不受支持


所以
(?我想知道,这个答案是否应该被接受。我完全同意,在我看来,使用捕获组比使用向后看/向前看要好。我使用LA/LB的唯一真正原因是在命令行上处理grep时,当我需要快速过滤某些内容时,可能偶尔使用grep或SED编写脚本,但我不这么认为使用它,即使是Perl也不行,因为在那一点上,不使用捕获不再有意义。此外,负面外观落后也很糟糕,因为它们必须是固定长度的;当您不知道字符串的确切长度时,这很烦人!我花了很长时间才第一次发现无法使用可变长度外观落后ds.我觉得我快发疯了。我可能应该重新审视这个答案,并提出一个完整的测试和解决方案,说明应该如何做。以下是我关于这个问题的更多答案:我想知道,这个答案是否应该被接受。我完全同意,在我看来,使用捕获组比使用“向后看/向前看”要好。我选择的唯一真正原因是er使用LA/LB是指在命令行上处理grep时,我需要快速过滤某些内容,可能偶尔在使用grep或SED编写脚本时使用,除此之外,我不使用它,甚至在Perl中也不使用它,因为在这一点上不再使用捕获是有意义的。此外,负面外观的落后也很糟糕,因为它们必须被修复当你不知道exa时,这很烦人