c++ - 用于 HTML 图像标签的 QRegExp

标签 c++ regex qt qregexp

首先,我只想说,我知道对 HTML 使用正则表达式是个坏主意。我只是用它来抓取 <img>标签信息,所以我不关心嵌套等。

也就是说,我正在尝试获取 src网页中所有图像的 URL。但是,我似乎只得到第一个结果。是我的正则表达式,还是我使用它的方式?我的正则表达式技能有点生疏,所以我可能遗漏了一些明显的东西。

QRegExp imgTagRegex("(<img.*>)+", Qt::CaseInsensitive); //Grab the entire <img> tag
imgTagRegex.setMinimal(true);
imgTagRegex.indexIn(pDocument);
QStringList imgTagList = imgTagRegex.capturedTexts();
imgTagList.removeFirst();   //the first is always the total captured text

foreach (QString imgTag, imgTagList) //now we want to get the source URL
{
    QRegExp urlRegex("src=\"(.*)\"", Qt::CaseInsensitive);
    urlRegex.setMinimal(true);
    urlRegex.indexIn(imgTag);
    QStringList resultList = urlRegex.capturedTexts();
    resultList.removeFirst();
    imageUrls.append(resultList.first());
}

当我点击 foreach 时循环,imgTagList仅包含 1 个字符串。对于“古埃及的猫”维基百科页面,它包含:

<img alt="" src="//upload.wikimedia.org/wikipedia/commons/thumb/1/13/Egypte_louvre_058.jpg/220px-Egypte_louvre_058.jpg" width="220" height="407" class="thumbimage" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/1/13/Egypte_louvre_058.jpg/330px-Egypte_louvre_058.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/1/13/Egypte_louvre_058.jpg 2x" />

这是我想要的,但我知道页面上有更 multimap 像标签...知道为什么我只得到第一个吗?


更新

在 Sebastian Lange 的帮助下,我能够做到这一点:

QRegExp imgTagRegex("<img.*src=\"(.*)\".*>", Qt::CaseInsensitive);
imgTagRegex.setMinimal(true);
QStringList urlMatches;
QStringList imgMatches;
int offset = 0;
while(offset >= 0)
{
    offset = imgTagRegex.indexIn(pDocument, offset);
    offset += imgTagRegex.matchedLength();

    QString imgTag = imgTagRegex.cap(0);
    if (!imgTag.isEmpty())
        imgMatches.append(imgTag); // Should hold complete img tag

    QString url = imgTagRegex.cap(1);
    if (!url.isEmpty())
    {
        url = url.split("\"").first(); //ehhh....
        if (!urlMatches.contains(url))
            urlMatches.append(url); // Should hold only src property
    }
}

split最后是摆脱 <img> 中的非 src 元素的 hacky 方法。标签,因为看起来我无法只获取 src="..." 中的数据部分。它有效,但这只是因为我无法获得正确的工作方式。我还添加了一些东西来标准化

最佳答案

QRegExp 通常只给出一次匹配。列表 capturedTexts() 给出了这一匹配的所有捕获!一个正则表达式语句中可以有多个捕获括号。要解决您的问题,您需要执行以下操作:

QRegExp imgTagRegex("\\<img[^\\>]*src\\s*=\\s*\"([^\"]*)\"[^\\>]*\\>", Qt::CaseInsensitive);
imgTagRegex.setMinimal(true);
QStringList urlmatches;
QStringList imgmatches;
int offset = 0;
while( (offset = imgTagRegex.indexIn(pDocument, offset)) != -1){
    offset += imgTagRegex.matchedLength();
    imgmatches.append(imgTagRegex.cap(0)); // Should hold complete img tag
    urlmatches.append(imgTagRegex.cap(1)); // Should hold only src property
}

编辑:将捕获 RegExpression 更改为 "\\<img[^\\>]*src=\"([^\"]*)\"[^\\>]*\\>" EDIT2:在 src 字符串中添加了可能的空格:"\\<img[^\\>]*src\\s*=\\s*\"([^\"]*)\"[^\\>]*\\>"

关于c++ - 用于 HTML 图像标签的 QRegExp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17689906/

相关文章:

qt - 是否有一种简单的方法来限制QObject::findChild()仅定向子级?

linux - 使用未安装 apt 的 KDE 框架和 Qt Creator

c - Gstreamer。对 `gst_video_overlay_set_window_handle' 的 undefined reference

c++ - 这两者的区别?

c++ - "addin.XLL is in a different format"问题,无法打开自己创建的xll

javascript - 删除特殊符号和多余空格并使其成为驼峰式 JavaScript?

c - 匹配不在引号内的模式

c++ - 如何调用子类函数实现的纯虚基类方法?

c++ - 使用 gcc 编译器时,未在此范围内声明“memcpy”

Javascript正则表达式,需要允许用户仅选择单词