regex_match找不到任何匹配项
regex_match doesn't find any matching
我在该站点上测试了RegEx [BCGRYWbcgryw]{4}[d]
,在以下BBCC[0]、GGRY[0]、WWWW[soln]中找到匹配似乎是可以的。它与BBCC[0]和GGRY[0]匹配。
但是,当我尝试对匹配进行编码和调试时,sm
值保持为空。
regex r("[BCGRYWbcgryw]{4}\[\d\]");
string line; in >> line;
smatch sm;
regex_match(line, sm, r, regex_constants::match_any);
copy(boost::begin(sm), boost::end(sm), ostream_iterator<smatch::value_type>(cout, ", "));
我哪里错了?
如果不想匹配整个输入序列,请使用std::regex_search
而不是std::regex_match
#include <iostream>
#include <regex>
#include <iterator>
#include <algorithm>
int main()
{
using namespace std;
regex r(R"([BCGRYWbcgryw]{4}[d])");
string line = "BBCC[0].GGRY[0].WWWW[soln]";
smatch sm;
regex_search(line, sm, r, regex_constants::match_any);
copy(std::begin(sm), std::end(sm), ostream_iterator<smatch::value_type>(cout, ", "));
cout << endl;
}
注意:这也使用原始字符串来简化正则表达式。
我终于开始工作了,用()
定义一个捕获组,用regex_iterator
找到所有与模式匹配的子字符串。
std::regex rstd("(\[[0-9]\].[BCGRYWbcgryw]{4})");
std::sregex_iterator stIterstd(line.begin(), line.end(), rstd);
std::sregex_iterator endIterstd;
for (stIterstd; stIterstd != endIterstd; ++stIterstd)
{
cout << " Whole string " << (*stIterstd)[0] << endl;
cout << " First sub-group " << (*stIterstd)[1] << endl;
}
输出为:
Whole string [0].GGRY
First sub-group [0].GGRY
Whole string [0].WWWW
First sub-group [0].WWWW
相关文章:
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- CMake 問題:找不到 Visual Studio 的任何執行個體
- 找不到 QRegularExpression 行为的任何解释。它有效,但不应该
- 给定一个 3 x 3 矩阵或任何 n x n 矩阵,为什么我的以下函数找不到对角线差异?
- ncurses 找不到任何关于cchar_t的文档
- 我想使用 "cout" 命令慢慢打印文本,但我真的找不到任何解决方案
- 这个基于范围的'for'语句需要一个合适的函数,但找不到任何函数
- 使用 Cmake 编译 LZO 时,它注意到找不到任何 VS 实例
- 如果找不到解决方案,我如何留下递归循环,而不会打印任何东西
- String.find() 在 C++ 中找不到任何内容时不会返回 -1
- Cereal 找不到任何输出序列化函数
- 错误的任何修复:二进制">>"找不到运算符
- 使用 libusb-win32 与 USB 到 DMX512 接口通信,找不到任何端点
- Hooking KiUserExceptionDispatcher- 在任何地方都找不到typedef
- ClearCommError中的串行通信在读取缓冲区中找不到任何数据
- OpenCV 2.4.2 calcOpticalFlowPyrLK 找不到任何点
- regex_match找不到任何匹配项
- 执行 SSE 内存复制导致的堆损坏 - CRT 检查找不到任何内容
- 编译错误:找不到标头、类和类接口的任何问题
- 错误:"{"标记之前的预期类名 - 似乎找不到任何循环包含