Extract value from key-value pair in substring

Hi,

I have a very large std::string (~500,000 characters) and need to extract certain smaller strings out of it (but only the value in a key-value pair). For example, here are some substrings in the larger string:

{"pp":"test"}

I only need value "test".

Once I've gotten "test", I need to find the *next* occurrence of "cc" (there will be multiple occurrences) and extract the value from there.

{"cc":"something with spaces"}

I only need value "something with spaces".


There will be multiple keys with the same name, so it would be great if I could either get all of them or choose which occurrence (by number).

I've been thinking about this for a while but have been unable to construct a proper solution. Would regex work for this?
Last edited on
Do you mean something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>

int main() {
    std::string s =
        "{\"pp\":\"alpha\"}..............."
            "{\"cc\":\"one two three\"}.........."
            "{\"cc\":\"four five\"},,,,,,,,,,,,,,,,,,"
        "{\"pp\":\"beta\"}..............."
            "{\"cc\":\"six seven\"}.........."
            "{\"cc\":\"eight\"}..................."
            "{\"cc\":\"nine\"},,,,,,,,,,,,,";

    int pp_count = 0;
    size_t start = s.find("\"pp\"");

    while (start != s.npos) {

        ++pp_count;
        start += 6;
        size_t end = s.find('"', start);
        if (end == s.npos) {
            std::cout << "can't find pp ending quote\n";
            break;
        }

        std::cout << pp_count << ": "
                  << s.substr(start, end - start) << '\n';

        int cc_count = 0;        
        size_t pp_next = s.find("\"pp\"", end);
        start = s.find("\"cc\"", end);

        while (start != s.npos && start < pp_next) {

            ++cc_count;
            start += 6;
            end = s.find('"', start);
            if (end == s.npos) {
                std::cout << "can't find cc ending quote\n";
                break;
            }
            std::cout << "    " << cc_count << ": "
                      << s.substr(start, end - start) << '\n';

            start = s.find("\"cc\"", end);
        }

        start = pp_next;
    }
}

Last edited on
Topic archived. No new replies allowed.