Monday 15 July 2013

c++ - Extract a specific text pattern from a string -



c++ - Extract a specific text pattern from a string -

i have string follows,

"0/41/9/71.94 pc:0x82cc (add)"

the desired output text between brackets ( )

ex: output = add,

for string specified above

how done using sscanf? there improve way in c++?

with string operations exclusively:

std::string text = "0/41/9/71.94 pc:0x82cc (add)"; auto pos = text.find('(') + 1; auto opcode = text.substr(pos, text.find(')', pos) - pos);

demo.

with sscanf this:

std::string opcode(5, '\0'); // suitable maximum size sscanf(text.c_str(), "%*[^(](%[^)]", &opcode[0]);

demo.

c++ string sscanf

No comments:

Post a Comment