C++无法regex_match工作
C++ can't get regex_match to work
在我的代码中,我想处理基于<>括号的字符串。对于这个,我想遍历字符串,一个接一个地替换括号,并根据括号内的内容做一些事情。
string msg = "This is an <red>Example<> message. For <blue>exampleness' sake<>.";
std::regex rexpr("<[a-zA-Z]*>");
// replace the first set of <> with %c, return the non-replaced version, and process it.
while(true){
std::smatch smatch;
// cant find any matches...
std::regex_match(msg, smatch, rexpr);
string key = smatch[0]; // this is empty from the start.
if(key.empty()) break; // no more keys, break.
// replace <...>
std::regex_replace(msg, rexpr, "%c", std::regex_constants::format_first_only);
if(key.size() == 2) continue; // closing brackets, nothing to process
// cut the brackets
key = key.substr(1, key.size() - 1);
// process the key.
// ...
}
需要用圆括号()括住要捕获的内容:
string msg = "This is an <red>Example<> message. For <blue>exampleness' sake<>.";
std::regex rexpr("(<[a-zA-Z]*>)");
smatch match;
if( regex_search(msg, match, rexpr) ) {
cout << match[0] << endl;
}
输出:<red>
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 基于FlannMatcher::match中的掩码无法正常工作
- 为什么第二次初始化工作,而第一次初始化由于"element type does not match"而失败?