[英]How to extract specific elements from a string?
I am trying to extract the first numbers from each block of numbers from the next string. 我试图从下一个字符串的每个数字块中提取第一个数字。
string s = "f 1079//2059 1165//2417 1164//2414 1068//1980";
In this example I need to extract 1079, 1165, 1164 and 1068 在此示例中,我需要提取1079、1165、1164和1068
I have tried with getline and substr but I have not been able to. 我已经尝试过使用getline和substr,但是我却不能。
You can utilize the <regex>
(C++ regular expression library) with pattern (\\\\d+)//
. 您可以将<regex>
(C ++正则表达式库)与模式(\\\\d+)//
。 Locate the numbers before double slashes. 在双斜杠前找到数字。 Also using the parentheses to extract the numbers only by submatch. 还使用括号仅通过子匹配来提取数字。
Here is usage. 这是用法。
string s = "f 1079//2059 1165//2417 1164//2414 1068//1980";
std::regex pattern("(\\d+)//");
auto match_iter = std::sregex_iterator(s.begin(), s.end(), pattern);
auto match_end = std::sregex_iterator();
for (;match_iter != match_end; match_iter++)
{
const std::smatch& m = *match_iter;
std::cout << m[1].str() << std::endl; // sub-match for token in parentheses, the 1079, 1165, ...
// m[0]: whole match, "1079//"
// m[1]: first submatch, "1070"
}
I usually reach for istringstream
for this kind of thing: 我通常会为这种事情寻求istringstream
帮助:
std::string input = "f 1079//2059 1165//2417 1164//2414 1068//1980";
std::istringstream is(input);
char f;
if (is >> f)
{
int number, othernumber;
char slash1, slash2;
while (is >> number >> slash1 >> slash2 >> othernumber)
{
// Process 'number'...
}
}
here is an attempt with getline and substring which works. 这是使用getline和substring的尝试。
auto extractValues(const std::string& source)
-> std::vector<std::string>
{
auto target = std::vector<std::string>{};
auto stream = std::stringstream{ source };
auto currentPartOfSource = std::string{};
while (std::getline(stream, currentPartOfSource, ' '))
{
auto partBeforeTheSlashes = std::string{};
auto positionOfSlashes = currentPartOfSource.find("//");
if (positionOfSlashes != std::string::npos)
{
target.push_back(currentPartOfSource.substr(0, positionOfSlashes));
}
}
return target;
}
Or there is another split way to extract tokens, but it may involve some string copy. 或者有另一种分离的方式来提取令牌,但是它可能涉及一些字符串复制。
Consider a split_by
function like 考虑如下的split_by
函数
std::vector<std::string> split_by(const std::string& str, const std::string& delem);
Possible implementations in Split a string in C++? 在C ++中拆分字符串的可能实现?
Make string be splitted by 使字符串被分割 first, then splitted by
//
and extract first item. 首先,然后由//
拆分并提取第一项。
std::vector<std::string> tokens = split_by(s, " ");
std::vector<std::string> words;
std::transform(tokens.begin() + 1, tokens.end(), // drop first "f"
std::back_inserter(words),
[](const std::string& s){ return split_by(s, "//")[0]; });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.