【问题标题】:How to find elements within string in C++如何在 C++ 中查找字符串中的元素
【发布时间】:2020-04-02 00:23:39
【问题描述】:

对于谷歌 URL,例如。 https://www.google.com/search?q=some+query&oq=some+query&aqs=chrome..69i57j0l5.2991j0j9&sourceid=chrome&ie=UTF-8

我需要抓取搜索引擎 (google.com) 任何查询(一些,查询) 以及所有选项(oq、aqs、sourceid 和 ie)

我假设我需要在搜索引擎的两个反斜杠之间找到字符。 但是,当有无数个查询并且我无法在要查找的字符串中定义星号和结尾时,我将如何找到每个查询?选项也是如此。

任何帮助将不胜感激。

【问题讨论】:

  • 请务必添加您需要帮助的语言。在您的问题中标记它会更好!了解您已经尝试过的内容会有所帮助。
  • 我建议使用能够解析 URL 的现有库,而不是尝试从头开始并可能不得不处理大量边缘情况。
  • 在您最喜欢的 C++ 参考中查找 std::string::find 方法。还要寻找“std::string::substr` 方法。
  • 第一个未转义的: 之前的所有内容都是方案,: 之后直到第一个未转义的? 是由未转义的/ 分隔的主机和路径。从? 到未转义的# 之后的所有内容都是查询字符串,它通常包含<name>=<value> 对,由未转义的& 字符分隔。 # 之后的所有内容都是一个片段。有关正式语法,请参阅 RFC 3986。拆分您显示的 URL 将为您提供以下组件:httpswww.google.com/searchq=some+queryoq=some+queryaqs=chrome..sourceid=chromeie=UTF-8

标签: c++ string find


【解决方案1】:

让我们使用您给出的示例:https://www.google.com/search?q=some+query&oq=some+query&aqs=chrome..69i57j0l5.2991j0j9&sourceid=chrome&ie=UTF-8

这里的总体思路是根据某些分隔符拆分字符串。在这种情况下,实际 URL 的各个部分是基于“/”划分的。如果我们以此为基础分割字符串,我们会得到:

  • https:
  • www.google.com
  • search?q=some+query&oq=some+query&aqs=chrome..69i57j0l5.2991j0j9&sourceid=chrome&ie=UTF-8

第三部分可以根据“&”进一步拆分给出:

  • 搜索?q=some+query
  • oq=some+query
  • aqs=chrome..69i57j0l5.2991j0j9
  • sourceid=chrome
  • ie=UTF-8

然后,您可以根据“=”拆分其中的每一个,以获取选项名称和选择的值。例如,“sourceid=chrome”将拆分为选项名称“sourceid”和值“chrome”。

在实际的 C++ 代码中实现如下:

// Includes
#include <iostream>
#include <vector>

// For simplicity's sake, we'll call std::vector<std::string> "Tokens"
using Tokens = std::vector<std::string>;

// Create a function to return an std::vector<std::string>
Tokens getTokens(std::string inputString, std::string delimiter)
{
    // Create a list of tokens
    Tokens tokens;

    // Create a string to store the curernt token
    std::string token;

    // Iterate through until there are no more delimiters (at which our token will be identical to our input string)
    while (token != inputString)
    {
        // Set the token to the section of the input string from its beginning to where the first delimiter is found
        token = inputString.substr(0, inputString.find(delimiter));

        // If that token isn't empty append it to the list of tokens (we could get any empty token if two delimiters follow one another, e.g. //)
        if (token != "")
        {
            tokens.push_back(token);
        }

        // Remove the token from the original input string
        inputString = inputString.substr(inputString.find(delimiter) + 1);
    }

    return tokens;
}

int main()
{
    // This is our URL:
    std::string URL = "https://www.google.com/search?q=some+query&oq=some+query&aqs=chrome..69i57j0l5.2991j0j9&sourceid=chrome&ie=UTF-8";

    // Split up the URL into tokens based on the delimiter "/"
    Tokens URL_tokens = getTokens(URL, "/"); // <--- should contain three tokens, "https:", "www.google.com" and everything after that

    // Split up the third token of the URL tokens based on the delimiter "&"
    Tokens details_tokens = getTokens(URL_tokens[2], "&");

    // Print out the tokens
    std::cout << URL_tokens[0] << "\n";
    std::cout << URL_tokens[1] << "\n\n";

    for (int token = 0; token < details_tokens.size(); token++)
    {
        // Split into option and value
        Tokens option_and_value = getTokens(details_tokens[token], "=");

        std::cout << "Option: " << option_and_value[0] << "\n";
        std::cout << "Value: " << option_and_value[1] << "\n\n";
    }
}

在我的电脑上,这产生了以下输出:

https:
www.google.com

Option: search?q
Value: some+query

Option: oq
Value: some+query

Option: aqs
Value: chrome..69i57j0l5.2991j0j9

Option: sourceid
Value: chrome

Option: ie
Value: UTF-8

【讨论】:

  • 谢谢!很抱歉回复晚了,但这很有帮助。
  • 不用担心 :) 如果需要,您可以进一步简化我的代码,但我确保以最易读的方式而不是尽可能最短的方式编写代码。此外,如果您认为这是您问题的正确答案,那么如果您接受它,我将不胜感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-17
  • 1970-01-01
  • 2021-06-05
  • 1970-01-01
  • 2015-08-22
相关资源
最近更新 更多