C++ 中的正则表达式和双反斜杠
Regex expression in C++ and double backslash
我正在读取一个格式为
的文本文件
People list
[Jane]
Female
31
...
对于每一行我想遍历并找到包含“[...]”的行
例如,[简]
我想到了正则表达式
"(^[\w+]$)"
我用 regex101.com 测试过它可以工作。
但是,当我尝试在我的代码中使用它时,它无法与任何东西匹配。
这是我的代码:
void Jane::JaneProfile() {
// read each line, for each [title], add the next lines into its array
std::smatch matches;
for(int i = 0; i < m_numberOfLines; i++) { // #lines in text file
std::regex pat ("(^\[\w+\]$)");
if(regex_search(m_lines.at(i), matches, pat)) {
std::cout << "smatch " << matches.str(0) << std::endl;
std::cout << "smatch.size() = " << matches.size() << std::endl;
} else
std::cout << "wth" << std::endl;
}
}
当我 运行 这段代码时,所有行都进入 else 循环并且没有任何匹配...
我搜索了答案,但当我看到 C++ 必须使用双反斜杠而不是一个反斜杠来转义时,我感到很困惑...但它对我的代码不起作用,即使我使用双反斜杠...
我哪里错了?
顺便说一句,我正在使用基于(桌面)Qt 5.5.1(Clang 6.1(Apple),64 位)的 Qt Creator 3.6.0
---编辑----
我试过:
std::regex pat (R"(^\[\w+\]$)");
但是我收到一条错误消息
Use of undeclared identifier 'R'
我已经有 #include <regex>
但我还需要添加其他内容吗?
转义反斜杠或使用带有不会出现在正则表达式中的前缀的原始字符版本:
逃脱:
std::regex pat("^\[\w+\]$");
原始字符串:
std::regex pat(R"regex(^\[\w+\]$)regex");
工作演示(改编自 OP 发布的代码):
#include <iostream>
#include <regex>
#include <sstream>
#include <string>
#include <vector>
int main()
{
auto test_data =
"People list\n"
"[Jane]\n"
"Female\n"
"31";
// initialise test data
std::istringstream source(test_data);
std::string buffer;
std::vector<std::string> lines;
while (std::getline(source, buffer)) {
lines.push_back(std::move(buffer));
}
// test the regex
// read each line, for each [title], add the next lines into its array
std::smatch matches;
for(int i = 0; i < lines.size(); ++i) { // #lines in text file
static const std::regex pat ("(^\[\w+\]$)");
if(regex_search(lines.at(i), matches, pat)) {
std::cout << "smatch " << matches.str() << std::endl;
std::cout << "smatch.size() = " << matches.size() << std::endl;
} else
std::cout << "wth" << std::endl;
}
return 0;
}
预期输出:
wth
smatch [Jane]
smatch.size() = 2
wth
wth
我正在读取一个格式为
的文本文件People list
[Jane]
Female
31
...
对于每一行我想遍历并找到包含“[...]”的行 例如,[简]
我想到了正则表达式
"(^[\w+]$)"
我用 regex101.com 测试过它可以工作。 但是,当我尝试在我的代码中使用它时,它无法与任何东西匹配。 这是我的代码:
void Jane::JaneProfile() {
// read each line, for each [title], add the next lines into its array
std::smatch matches;
for(int i = 0; i < m_numberOfLines; i++) { // #lines in text file
std::regex pat ("(^\[\w+\]$)");
if(regex_search(m_lines.at(i), matches, pat)) {
std::cout << "smatch " << matches.str(0) << std::endl;
std::cout << "smatch.size() = " << matches.size() << std::endl;
} else
std::cout << "wth" << std::endl;
}
}
当我 运行 这段代码时,所有行都进入 else 循环并且没有任何匹配...
我搜索了答案,但当我看到 C++ 必须使用双反斜杠而不是一个反斜杠来转义时,我感到很困惑...但它对我的代码不起作用,即使我使用双反斜杠... 我哪里错了?
顺便说一句,我正在使用基于(桌面)Qt 5.5.1(Clang 6.1(Apple),64 位)的 Qt Creator 3.6.0
---编辑----
我试过:
std::regex pat (R"(^\[\w+\]$)");
但是我收到一条错误消息
Use of undeclared identifier 'R'
我已经有 #include <regex>
但我还需要添加其他内容吗?
转义反斜杠或使用带有不会出现在正则表达式中的前缀的原始字符版本:
逃脱:
std::regex pat("^\[\w+\]$");
原始字符串:
std::regex pat(R"regex(^\[\w+\]$)regex");
工作演示(改编自 OP 发布的代码):
#include <iostream>
#include <regex>
#include <sstream>
#include <string>
#include <vector>
int main()
{
auto test_data =
"People list\n"
"[Jane]\n"
"Female\n"
"31";
// initialise test data
std::istringstream source(test_data);
std::string buffer;
std::vector<std::string> lines;
while (std::getline(source, buffer)) {
lines.push_back(std::move(buffer));
}
// test the regex
// read each line, for each [title], add the next lines into its array
std::smatch matches;
for(int i = 0; i < lines.size(); ++i) { // #lines in text file
static const std::regex pat ("(^\[\w+\]$)");
if(regex_search(lines.at(i), matches, pat)) {
std::cout << "smatch " << matches.str() << std::endl;
std::cout << "smatch.size() = " << matches.size() << std::endl;
} else
std::cout << "wth" << std::endl;
}
return 0;
}
预期输出:
wth
smatch [Jane]
smatch.size() = 2
wth
wth