C++中从指定格式字符串提取Count的动态目标数值
问题描述
原始字符串格式如下:
"---Start of list---\nSaved Value -> Current Value\nlvlpin_dots: 0 -> 2\nTwinkle2: 0 -> 3\nlvlpin_10k_rays_smudge: 0 -> 2\nfirst_attempt_tingling_004: 0 -> 2\nboosterwheel_spinning_stars: 0 -> 1\nActiveLevel3D: 0 -> 1\nfirst_attempt_tingling_003: 0 -> 2\nActiveLevel3D_2: 0 -> 1\nlvlpin_10k_rays: 0 -> 2\nActiveLevel3D_3: 0 -> 1\nads_glitters_hears: 0 -> 1\nlvlpin_10k_twinkles: 0 -> 2\nads_dust_hearts: 0 -> 1\nCount: 0 -> 21\n---End of list---"
去除所有空格后得到:
---Startoflist---SavedValue->CurrentValuelvlpin_dots:0->2Twinkle2:0->3lvlpin_10k_rays_smudge:0->2first_attempt_tingling_004:0->2boosterwheel_spinning_stars:0->1ActiveLevel3D:0->1first_attempt_tingling_003:0->2ActiveLevel3D_2:0->1lvlpin_10k_rays:0->2ActiveLevel3D_3:0->1ads_glitters_hears:0->1lvlpin_10k_twinkles:0->2ads_dust_hearts:0->1Count:0->21---Endoflist---
需求:提取Count:X->Y中的Y值(X为动态变化的数值)。
尝试的代码未得到预期结果:
std::string::size_type dPos = particleValueStart.find("Count"); std::string count = particleValueStart.substr (dPos); Log("Count num is: %s", count);
解决方案
方法一:基于字符串定位截取
通过逐步定位关键标记的位置,精准提取目标数值:
#include <string> void extractCountValue(const std::string& str) { // 定位Count字段起始位置 size_t countPos = str.find("Count:"); if (countPos == std::string::npos) { Log("Count field not found"); return; } // 跳过"Count:",找到箭头"->"的位置 size_t arrowPos = str.find("->", countPos + 6); if (arrowPos == std::string::npos) { Log("Arrow symbol missing in Count field"); return; } // 找到末尾结束标记的位置 size_t endMarkerPos = str.find("---Endoflist---", arrowPos); if (endMarkerPos == std::string::npos) { Log("End marker not found"); return; } // 提取箭头到结束标记之间的数值子串 std::string countValue = str.substr(arrowPos + 2, endMarkerPos - (arrowPos + 2)); Log("Count num is: %s", countValue.c_str()); // 若需要转为整数: // int num = std::stoi(countValue); }
方法二:正则表达式匹配(C++11及以上)
利用正则表达式灵活匹配动态格式:
#include <string> #include <regex> void extractCountValue(const std::string& str) { // 匹配Count:数字->数字的格式,捕获箭头后的数字 std::regex countRegex(R"(Count:\d+->(\d+))"); std::smatch match; if (std::regex_search(str, match, countRegex)) { std::string countValue = match[1].str(); Log("Count num is: %s", countValue.c_str()); // 转为整数: // int num = std::stoi(countValue); } else { Log("Count field not found or format invalid"); } }
方法三:反向查找优化(高效定位末尾字段)
由于Count字段靠近字符串末尾,反向查找缩小范围提升效率:
#include <string> void extractCountValue(const std::string& str) { // 先定位末尾结束标记 size_t endMarkerPos = str.find("---Endoflist---"); if (endMarkerPos == std::string::npos) { Log("End marker not found"); return; } // 从结束标记向前查找Count字段 size_t countPos = str.rfind("Count:", endMarkerPos); if (countPos == std::string::npos) { Log("Count field not found"); return; } size_t arrowPos = str.find("->", countPos + 6); if (arrowPos == std::string::npos || arrowPos >= endMarkerPos) { Log("Arrow symbol missing in Count field"); return; } std::string countValue = str.substr(arrowPos + 2, endMarkerPos - (arrowPos + 2)); Log("Count num is: %s", countValue.c_str()); }
内容的提问来源于stack exchange,提问作者user21313886
相关产品推荐
相关产品推荐

