正则表达式可以,但不工作
regex ok but does not works
这段代码不工作,为什么?
std::cmatch result;
std::string str("trucmuch.servicen Loaded: not-found (Reason: No such file or directory)n Active: inactive (dead)... (101)");
std::regex rgx("Loaded:s+(S+)(?:s+((?:Reason:s*)?([^)]*)))?", std::regex::ECMAScript);
std::regex_match(str.c_str(), result, rgx);
qDebug() << result.size();
显示0 !!
如何获得result[0]和result1 ("not-found", "No such file or directory")?
regex101的测试
使用std::regex_search
查找与模式匹配的子字符串。您还需要正确地转义反斜杠,或者更好地使用原始字符串文字。以下作品:
#include <iostream>
#include <regex>
#include <string>
int main()
{
std::smatch result;
std::string str =
"trucmuch.servicen Loaded: not-found (Reason: No such file "
"or directory)n Active: inactive (dead)... (101)";
std::regex rgx(R"(Loaded:s+(S+)(?:s+((?:Reason:s*)?([^)]*)))?)",
std::regex::ECMAScript);
std::regex_search(str, result, rgx);
for (const auto & sm : result) { std::cout << sm << 'n'; }
}
相关文章:
- 使用正则表达式regex_search在字符串中查找字符串
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- C++正则表达式无限循环
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 定义有趣的宏和正则表达式在Z3 C++绑定
- 带有多个字符分隔符的正则表达式
- C++ 使用增强正则表达式库时断言崩溃
- 如何使HTML5电子邮件验证正则表达式在C++中工作?
- C++ 正则表达式未按预期工作 (regex_search)
- 正则表达式代码无缘无故停止工作
- 如何在 QT 框架工作C++中使用变量作为正则表达式模式
- C++ 中的正则表达式语句未按预期工作
- 通过getline()输入,正则表达式无法正常工作
- C++正则表达式与PCRE表达式一起不工作
- std::正则表达式无法按预期工作
- 带有 Qt 的正则表达式 - indexIn(const QString &) 无法按预期工作
- 在c++正则表达式中不工作
- 正则表达式可以,但不工作
- 正则表达式库在c++中不能正常工作
- 整数匹配正则表达式模式不工作