I may have the following inputs:
<Object::1 <1,2><3,4><3,3>>
<Object::2 <1,2><3,4>>
<Object::3 <1,2> 5>
I need two extract the value after :: (may be a string), and then the i number of values after in the <>.
So for the first example, I'd want to get:
1 <1,2> <3,4> <3,3>
I can read from the string, I'm just not sure how to get what I want from it?
我认为这应该对您有用(?<=::)([^<]+)\\s+.*?(<.*)>
演示
Hmmm, let's first read the input line into a string.
std::string text_from_file;
getline(my_text_file, text_from_file);
You want to "skip" past the text " This involves using the find
method of std::string
.
std::string::size_type position_in_string;
position_in_string = text_from_file.find("<Object::");
Next, test the position. After all, we don't want to continue if the key string wasn't found.
if (position_in_string != std::string::npos)
{
The next tricky part is to get the number following the "::".
This can be done many ways, we'll try std::istringstream
. We need to get the text out of the string after the "::", which is called a substring , abbreviated as substr
.
unsigned int quantity = 0;
std::istringstream string_input(text_from_file.substr(position_in_string));
We can use the stream extraction operators on the text string:
string_input >> quantity;
Getting the remaining text from the input text is called parsing. Very similar to the operations above. You give it a try.
There are many posts in StackOverflow that can be very helpful. Find them by searching for "[C++] parsing read from file".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.