没有LookBehind功能的正则表达式

问题描述 投票:1回答:2

我正在尝试编写一个查找所有';'的正则表达式未跟随NEW LINE(\ n)字符的字符。

;(?!\\\n)

和所有NEW LINE(\ n)字符前面没有';'字符:

(?< !;)\\\n

不幸的是我使用Qt 4.7.4 QRegExp并且它不支持“Look Behind”。如何重写上面的正则表达式,以便它不使用“Look Behind”?

qt qregexp
2个回答
1
投票

引用文档:

http://doc.qt.digia.com/4.7/qregexp.html#details

使用与Perl相同的语法支持零宽度正和零宽度负前瞻断言(?=模式)和(?!模式)。

可能发生的是你运行的Windows机器上插入了\r\n而不仅仅是\n ...或者它可能是在Windows机器上创建的文本文件。

我需要注意的一件事是,我发现了外观,你不能拥有大多数正则表达式处理程序的可变长度。

如果lookbehinds / lookaheads仍然给你带来麻烦,另一个选择的选项是使用捕获组,然后只引用你感兴趣的捕获组。

从文档的code-examples section它有这个:

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个索引是整个匹配(不分为捕获组)。

http://doc.qt.digia.com/4.7/qregexp.html#cap

http://doc.qt.digia.com/4.7/qregexp.html#capturedTexts

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

我也喜欢使用这个tool。格式可能与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();
}

输出:

Pattern "(;)([^\n])"
         0 Found at position 104 ("; ", ";", " ")
         1 Found at position 126 (";)", ";", ")")
         2 Found at position 169 (";)", ";", ")")
         3 Found at position 247 (";]", ";", "]")
         4 Found at position 295 (";)", ";", ")")
Pattern "(;)(?!\n)"
         0 Found at position 104 (";", ";")
         1 Found at position 126 (";", ";")
         2 Found at position 169 (";", ";")
         3 Found at position 247 (";", ";")
         4 Found at position 295 (";", ";")
Pattern "([^;])(\n)"
         0 Found at position 123 (".\n", ".", "\n")
         1 Found at position 166 ("e\n", "e", "\n")
         2 Found at position 242 ("d\n", "d", "\n")
         3 Found at position 289 ("e\n", "e", "\n")
         4 Found at position 347 (".\n", ".", "\n")
Pattern "(?<!;)(\n)"
        No matches found.

0
投票

Perl的lookbehind断言,“独立”子表达式和条件表达式不受支持。

来自http://doc.qt.io/archives/qt-4.8/qregexp.html

所以(?<;!;)\n不起作用 并且(?!;)\n将匹配所有新行字符 无论他们之前是否有;

© www.soinside.com 2019 - 2024. All rights reserved.