【问题标题】:Extracting the filename off a URL with QRegExp使用 QRegExp 从 URL 中提取文件名
【发布时间】:2016-09-22 19:25:01
【问题描述】:

我有一个问题,我有一个 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 中提取文件名,但不返回任何内容。我不确定出了什么问题。有人可以提供帮助吗?提前谢谢你。

【问题讨论】:

  • 你为什么要\\.?原来只有一个.
  • 抱歉,乔尼!显然我太累了,应该睡着了:
  • 好吧,我的意思是写在下面:[^/\\&\?]+\.\w{3,4}(?=([\?&].*$ |$))

标签: c++ regex qt qregexp


【解决方案1】:

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"

【讨论】:

  • 谢谢!这是我的问题的解决方案^ ^
猜你喜欢
  • 2015-03-19
  • 1970-01-01
  • 2010-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多