没有LookBehind功能的正则表达式

时间:2013-04-02 00:49:32

标签: qt qregexp

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

;(?!\\\n)

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

(?< !;)\\\n

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

2 个答案:

答案 0 :(得分:1)

从文档中引用:

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

  

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

可能发生的事情是您在已插入\r\n而不仅仅是\n的Windows计算机上运行...或者它可能是在Windows计算机上创建的文本文件。< / p>

我要注意的一件事是,我发现了外观,是因为大多数正则表达式处理程序都没有可变长度的外观。

如果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.

答案 1 :(得分:0)

  

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

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

因此<div class="slds-form--stacked slds-grid slds-wrap" role="form"> <div class="slds-form-element slds-m-top--large slds-size--3-of-6"> <label class="slds-form-element__label dsLabel" for="selectSample1">Destination Salesforce Organization</label> <div class="slds-form-element tooltipIcon"> <div class="slds-form-element__icon slds-align-middle"> <img alt="Table" class="slds-button__icon" src="/resource/1530008060000/pdri_sprint17__SLDS_Resource/assets/icons/utility/info_60.png"> </div> <div class="slds-popover slds-popover--tooltip slds-nubbin--left-top toggle slds-hide" id="tooltipDestination" role="tooltip" style="position: absolute;top: -49px;left: 30px;float: left;min-width: 312px;"> <div class="slds-popover__body">Specifies the organization to copy data to. Use Ctrl+click to select up to five destinations.</div> </div> </div> <div class="slds-form-element__control slds-m-top--small"><select id="j_id0" name="j_id0:theForm:targetList" class="slds-select slds-select1" multiple="multiple" size="3"> <option value="a02m00000086vRZAAY">Local connection</option> <option value="a02m00000086vReAAI">Destination connection</option> </select> <input id="valholder" type="hidden" name="selval"> </div> </div> </div>不起作用
并且(?<;!;)\n将与所有换行符匹配
 不管它们前面是否有(?!;)\n