使用QRegExp从URL中提取文件名

Extracting the filename off a URL with QRegExp

本文关键字:提取 文件名 URL QRegExp 使用      更新时间:2023-10-16

我有一个问题,我有一个RegEx, [^/&?]+.w{3,4}(?=([?&].*$|$)),但我不能让它与下面的[1]函数一起工作。

[1] - http://doc.qt.io/qt-5/qregexp.html

这是我试过的代码:

QRegExp rx("[^/\\&\?]+\.\w{3,4}(?=([\?&].*$|$))", Qt::CaseInsensitive, QRegExp::RegExp);
std::ostringstream list;
int pos = 0;
while ((pos = rx.indexIn(url, pos)) != -1) {
    list << rx.cap(1).toStdString();
    pos += rx.matchedLength();
}
return list;

它应该从URL中提取文件名,但只返回什么。我不知道哪里出了问题。有人能帮忙吗?

Qt有QUrl类解析URL等。还有QUrl::fileName方法:

QUrl url("http://qt-project.org/support/file.html");
// url.adjusted(RemoveFilename) == "http://qt-project.org/support/"
// url.fileName() == "file.html"