Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.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
QRegexp和百分比(%)符号_Regex_Qt_Qregexp - Fatal编程技术网

QRegexp和百分比(%)符号

QRegexp和百分比(%)符号,regex,qt,qregexp,Regex,Qt,Qregexp,我正在尝试匹配模板中格式为%foo%的字符串。上下文:目标是用存储过程返回中foo列的值替换%foo% 我做不到。一开始我认为模板的UTF8编码是我麻烦的根源。但即使是以下几点也不成功: #include <QCoreApplication> #include <QRegExp> #include <iostream> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv);

我正在尝试匹配模板中格式为%foo%的字符串。上下文:目标是用存储过程返回中foo列的值替换%foo%

我做不到。一开始我认为模板的UTF8编码是我麻烦的根源。但即使是以下几点也不成功:

#include <QCoreApplication>
#include <QRegExp>
#include <iostream>

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

    QString str = "__%foo%____";


    std::cout << "with regex: %(.*)%" << std::endl;
    QRegExp re("%(.*)%",Qt::CaseInsensitive);
    re.indexIn(str);
    for(int pos = 0; pos < re.captureCount(); ++pos)
    {
        std::cout << re.cap(pos).toStdString() << std::endl;
    }

    std::cout << "with regex: \\%(.*)\\%" << std::endl;
    QRegExp re2("\\%(.*)\\%",Qt::CaseInsensitive);
    re2.indexIn(str);
    for(int pos = 0; pos < re2.captureCount(); ++pos)
    {
        std::cout << re2.cap(pos).toStdString() << std::endl;
    }

    std::cout << "with regex: %([^%])%" << std::endl;
    QRegExp re3("%([^%])%",Qt::CaseInsensitive);
    re3.indexIn(str);
    for(int pos = 0; pos < re3.captureCount(); ++pos)
    {
        std::cout << re3.cap(pos).toStdString() << std::endl;
    }

    std::cout << "with regex:  \\%([^\\%])\\%" << std::endl;
    QRegExp re4("\\%([^\\%])\\%",Qt::CaseInsensitive);
    re4.indexIn(str);
    for(int pos = 0; pos < re4.captureCount(); ++pos)
    {
        std::cout << re4.cap(pos).toStdString() << std::endl;
    }

    std::cout << "with regex: \\x25([^\\x25])\\x25" << std::endl;
    QRegExp re5("\\x25([^\\x25])\\x25",Qt::CaseInsensitive);
    re5.indexIn(str);
    for(int pos = 0; pos < re5.captureCount(); ++pos)
    {
        std::cout << re5.cap(pos).toStdString() << std::endl;
    }

    std::cout << "with regex: \\%(.*)\\%" << std::endl;
    QRegExp re6("\\%(.*)\\%",Qt::CaseInsensitive);
    re6.indexIn(str);
    for(int pos = 0; pos < re6.captureCount(); ++pos)
    {
        std::cout << re6.cap(pos).toStdString() << std::endl;
    }

    return a.exec();
}

我只想捕获foo,而不是“%”ok

int pos = 0; pos < re.captureCount(); ++pos

cap(0)与整个表达式明显匹配

使用非捕获组:

QString txt="____%foo%___some%__";
QRegExp rx("(?:%)[^%]*(?:%)");
pos = rx.indexIn(txt, 0);
rx.capturedTexts().at(1); //holds foo

如果您需要所有匹配项,请使用indexIn循环并提供pos+rx.matchedLength(),而不是0

从您的问题中删除答案,并将其正确地放在下面的答案部分。上一次人们要求我做相反的操作:D但很好,我将保留这两个选项。无意义。答案不是问题,因此问题不应该包含答案。顺便说一句,过一段时间你就可以接受自己的答案了。对于大多数问题,答案都是从不同的答案中收集出来的,并放在问题编辑的顶部。
int pos = 0; pos <= re.captureCount(); ++pos
with regex: %(.*)%
%foo%
foo
with regex: \%(.*)\%
%foo%
foo
with regex: %([^%])%


with regex:  \%([^\%])\%


with regex: \x25([^\x25])\x25


with regex: \%(.*)\%
%foo%
foo
QString txt="____%foo%___some%__";
QRegExp rx("(?:%)[^%]*(?:%)");
pos = rx.indexIn(txt, 0);
rx.capturedTexts().at(1); //holds foo