标准c++中的Regex
Regex in std c++
我想找到类似"{some text}"的所有事件。
我的代码是:
std::wregex e(L"(\{([a-z]+)\})");
std::wsmatch m;
std::regex_search(chatMessage, m, e);
std::wcout << "matches for '" << chatMessage << "'n";
for (size_t i = 0; i < m.size(); ++i) {
std::wssub_match sub_match = m[i];
std::wstring sub_match_str = sub_match.str();
std::wcout << i << ": " << sub_match_str << 'n';
}
但对于这样的字符串:L"玫瑰{aaa}{bbb}是{ccc}#ff0000")我的输出是:
0: {aaa}
1: {aaa}
2: aaa
并且我没有得到下一个子串。我怀疑我的正则表达式有问题。你们中有人知道出了什么问题吗?
您只需搜索一次,然后简单地在组中循环。相反,您需要多次搜索并只返回正确的组。尝试:
std::wregex e(L"(\{([a-z]+)\})");
std::wsmatch m;
std::wcout << "matches for '" << chatMessage << "'n";
while (std::regex_search(chatMessage, m, e))
{
std::wssub_match sub_match = m[2];
std::wstring sub_match_str = sub_match.str();
std::wcout << sub_match_str << 'n';
chatMessage = m.suffix().str(); // this advances the position in the string
}
这里的2
是第二组,即括号中的第二个东西,即([a-z]+)
。
有关组的更多信息,请参阅此。
正则表达式没有任何问题,但您需要反复搜索它。而且你根本不需要括号。
std::regex_search
发现模式的一次出现。这是{aaa}
。std::wsmatch
就是这样。它有3个子表。整个字符串、外括号的内容(再次是整个字符串)和内括号的内容。这就是你所看到的。
您必须在字符串的其余部分再次调用regex_search
才能获得下一个匹配项:
std::wstring::const_iterator begin = chatMessage.begin(), end = chatMessage.end();
while (std::regex_search(begin, end, m, e)) {
// ...
begin = m.end();
}
regex_match
对象上的索引运算符返回该索引处的匹配子字符串。当索引为0时,它返回整个匹配字符串,这就是为什么输出的第一行是{aaa}
。当索引为1时,它返回第一个捕获组的内容,即正则表达式中位于第一个(
和对应的)
之间的部分所匹配的文本。在本例中,这些是最外面的括号,它再次生成{abc}
。当索引为2时,返回第二个捕获组的内容,即第二个(
与其对应的)
之间的文本,该文本为aaa
。
从您停止的地方再次搜索最简单的方法是使用迭代器:
std::wsregex_iterator it(chatMessage.begin(), chatMessage.end(), e);
for ( ; it != wsregex_iterator(); ++it) {
std::cout << *it << 'n';
}
(注意:这是一个草图,未经测试)
相关文章:
- 使用Regex解析cpp中的字符串并创建映射
- CMake中的奇怪字符串(REGEX REPLACE)行为
- 如何使用C 中的Regex在给定句子中的多个空格隔开的两个单词匹配
- 我可以在regex中使用带有or运算符的前瞻性断言吗
- VC 中的Regex拆分字符串
- 如何使用C 中的Regex替换文件名的扩展
- Boost Regex中的命名子表达式错误
- Regex只匹配匹配组中许多可能性中的前两个
- 如何使用 std::regex 查找字符串中的下一个匹配项
- Boost::Regex在C++中的使用
- 如何在 boost::regex 中找到子匹配的字符串位置
- 防止解释 std::regex 中的特殊字符
- c++中的Regex用于接受“&”;
- C++中的Regex,从正则表达式中获取文本任何部分的字符串
- 应该如何使用C++11 Regex从存储在文本文件中的表中创建变量
- Regex字符串在已分析字符串中的位置
- 标准c++中的Regex
- std::regex中的错误
- c++ regex中的字符类不能正常工作
- 如何迭代std::string中所有匹配的正则表达式,使其在c++11 std::regex中的起始位置