Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.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

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
C++ 使用QRegExp的Ingore字符串_C++_Qt_Http_Https_Qregexp - Fatal编程技术网

C++ 使用QRegExp的Ingore字符串

C++ 使用QRegExp的Ingore字符串,c++,qt,http,https,qregexp,C++,Qt,Http,Https,Qregexp,我有一个以下格式的字符串 qString路径= 我想使用QRegExp从上述路径输入用户名和密码。 一名律师也处理了以下案件 1。qString路径=http://user:pass@someurl。 在以下情况下,如果不包含任何用户名或密码,则返回字符串 2. qString path = https://someurl.com 我的代码是用http和https编写的,有没有最好的方法可以做到这一点呢。请建议 f(Path.startsWith("https://") == true) {

我有一个以下格式的字符串

qString路径=

我想使用QRegExp从上述路径输入用户名和密码。 一名律师也处理了以下案件

1。qString路径=http://user:pass@someurl。

在以下情况下,如果不包含任何用户名或密码,则返回字符串

2. qString path = https://someurl.com
我的代码是用http和https编写的,有没有最好的方法可以做到这一点呢。请建议

f(Path.startsWith("https://") == true)
{
    QRegExp UserPwd("(.*)(https://)(.*)(.*)", Qt::CaseInsensitive, QRegExp::RegExp);
    QRegExp UserPwd1("(.*)(https://)(.*)@(.*)", Qt::CaseInsensitive, QRegExp::RegExp);

    if(UserPwd1.indexIn(ErrorString) != -1)
    {
        (void) UserPwd1.indexIn(Path);
        return UserPwd1.cap(1) + UserPwd1.cap(2) + UserPwd1.cap(4);
    }
    else
    {
        (void) UserPwd.indexIn(Path);
        return UserPwd.cap(1) + UserPwd.cap(2) + UserPwd.cap(3);
    }
}
else
{
    QRegExp UserPwd("(.*)(http://)(.*)@(.*)", Qt::CaseInsensitive, QRegExp::RegExp);
    (void) UserPwd.indexIn(Path);
    return UserPwd.cap(1) + UserPwd.cap(2) + UserPwd.cap(4);
}

它可以通过使用

下面的函数操作URL格式

那就叫它

QUrl oUrl("https://user:pass@someurl.com");

std::cout<< GetFixedUrl(oUrl).toString().toStdString()<< std::endl;

我建议两种方法。您可以选择一个更方便、更适合您的:

使用正则表达式

QString removeAuthority1(const QString &path)
{
  QRegExp rx("((http|https|ftp):\\/\\/)(.*@)?(.*)");
  if (rx.indexIn(path) != -1) {
    return rx.cap(1) + rx.cap(4);
  }
  return QString();
}
使用QUrl

QString removeAuthority2(const QString &path)
{
  QUrl url = QUrl::fromUserInput(path);
  return url.scheme() + "://" + url.host();
}
用法

QString path("http://user:pass@someurl.com");
QString s1 = removeAuthority1(path); // http://someurl.com
QString s2 = removeAuthority2(path); // http://someurl.com

为什么要使用QRegExp执行此操作?使用QUrl。在
UserPwd
中,组4将始终为空(因为组3将始终消耗所有剩余的text@Jonas,我想在运行时区分什么是http或https,它是否包含用户名或密码。这就是我使用QRegExp的原因。@PrabhatChauhan毫不奇怪,QUrl(作为一个专门为处理URL而设计的类)有这样的功能!如果我的路径包含一些文本示例“如何从路径提取用户名和密码”,会发生什么。我想删除用户名和密码。在这种情况下,上述方法不起作用。应使用正则表达式方法。但是,您的路径仍应以
http://
QString removeAuthority2(const QString &path)
{
  QUrl url = QUrl::fromUserInput(path);
  return url.scheme() + "://" + url.host();
}
QString path("http://user:pass@someurl.com");
QString s1 = removeAuthority1(path); // http://someurl.com
QString s2 = removeAuthority2(path); // http://someurl.com