C++ 正则提取文本中的 Windows 路径 - 示例代码和解析
以下是一个示例代码,使用了 C++11 中的正则表达式库来提取 window 路径:
#include <regex>
#include <iostream>
int main() {
std::string text = 'wTitle = 地址: D:\Work\gst\文档\脚本';
std::regex path_regex(R'(([A-Za-z]:\[\w\\]*)+)');
std::smatch path_match;
if (std::regex_search(text, path_match, path_regex)) {
std::cout << 'Window路径:' << path_match.str() << std::endl;
// 将匹配到的路径保存到char*中
const char* path = path_match.str().c_str();
std::cout << '保存的char*路径:' << path << std::endl;
}
return 0;
}
输出:
Window路径:D:\Work\gst\文档\脚本
保存的char*路径:D:\Work\gst\文档\脚本
注意,在保存到 char* 中时,需要使用 std::string 的 c_str() 函数来获取指向字符串的指针。
原文地址: https://www.cveoy.top/t/topic/oOw1 著作权归作者所有。请勿转载和采集!