Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;11正则表达式搜索不起作用,但正则表达式匹配不起作用 < >我试图用C++中的正则表达式从文件中解析字符串,但是当我从文件读取字符串时,ReXEXY搜索不起作用: string stream; ifstream file("weather.txt"); stream.assign( (istreambuf_iterator<char>(file)), (istreambuf_iterator<char>())); const string s = stream; regex rgx("[0-9]{1,2}-[0-9]{1,2}"); smatch match; for(i=0; i < 12; i++){ if (regex_search(s, match, rgx)) cout << "match: " << match[i] << '\n'; } 字符串流; ifstream文件(“weather.txt”); assign((istreambuf_迭代器(文件)), (istreambuf_迭代器()); 常量字符串s=流; 正则表达式rgx(“[0-9]{1,2}-[0-9]{1,2}”); 小比赛; 对于(i=0;i_Regex_C++11 - Fatal编程技术网

C++;11正则表达式搜索不起作用,但正则表达式匹配不起作用 < >我试图用C++中的正则表达式从文件中解析字符串,但是当我从文件读取字符串时,ReXEXY搜索不起作用: string stream; ifstream file("weather.txt"); stream.assign( (istreambuf_iterator<char>(file)), (istreambuf_iterator<char>())); const string s = stream; regex rgx("[0-9]{1,2}-[0-9]{1,2}"); smatch match; for(i=0; i < 12; i++){ if (regex_search(s, match, rgx)) cout << "match: " << match[i] << '\n'; } 字符串流; ifstream文件(“weather.txt”); assign((istreambuf_迭代器(文件)), (istreambuf_迭代器()); 常量字符串s=流; 正则表达式rgx(“[0-9]{1,2}-[0-9]{1,2}”); 小比赛; 对于(i=0;i

C++;11正则表达式搜索不起作用,但正则表达式匹配不起作用 < >我试图用C++中的正则表达式从文件中解析字符串,但是当我从文件读取字符串时,ReXEXY搜索不起作用: string stream; ifstream file("weather.txt"); stream.assign( (istreambuf_iterator<char>(file)), (istreambuf_iterator<char>())); const string s = stream; regex rgx("[0-9]{1,2}-[0-9]{1,2}"); smatch match; for(i=0; i < 12; i++){ if (regex_search(s, match, rgx)) cout << "match: " << match[i] << '\n'; } 字符串流; ifstream文件(“weather.txt”); assign((istreambuf_迭代器(文件)), (istreambuf_迭代器()); 常量字符串s=流; 正则表达式rgx(“[0-9]{1,2}-[0-9]{1,2}”); 小比赛; 对于(i=0;i,regex,c++11,Regex,C++11,您必须使用迭代器(指针)前进到下一个开始位置 在字符串内 范例 std::string::const_iterator start, end; start = s.begin(); end = s.end(); smatch m; while ( regex_search( start, end, m, rx ) ) { // do something with the results std::string sgrp1(m[1].first, m[1].second);


您必须使用迭代器(指针)前进到下一个开始位置
在字符串内

范例

std::string::const_iterator start, end;
start = s.begin();
end   = s.end();
smatch m;

while ( regex_search( start, end, m, rx ) )
{
    // do something with the results
    std::string sgrp1(m[1].first, m[1].second);
    std::string sgrp2(m[2].first, m[2].second);
    std::string sgrp3(m[3].first, m[3].second);
    // etc, ...

    // update search position: 
    start = m[0].second;
} 
实际上,您正在尝试重新匹配整个字符串
12次,以便一次显示一个单独的12组,
这不是我们要走的路

如果您确实有12个组,则只需匹配一次,然后迭代
我只看了一次比赛结果

if ( regex_search( start, end, m, rx ) )
{
    for ( int i = 0; i < 12; i++ )
    {
         std::string str(m[i].first, m[i].second);
         cout << "group " << i << " = " << str << "\r\n";
    }
 }
if(正则表达式搜索(开始、结束、m、接收))
{
对于(int i=0;i<12;i++)
{
std::string str(m[i]。第一,m[i]。第二);

非常感谢您的回答,我还不能测试它,但我不太了解第一个示例中的字符串组。每个字符串组都是搜索中的匹配项?m的第一个和第二个对象到底是什么?抱歉,这是我第一次使用这个正则表达式库,我还不熟悉它。我尝试了两个选项,但都失败了直到regex_search不断返回false:(
match_results
是子表达式匹配的索引集合。
sub_match
类型的对象只能通过订阅match_results类型的对象来获得。即
m[I]
。每个子匹配表示regex中定义的组,如
(abc)(def)(ghi)(jk)
有4个组。但是您的正则表达式
[0-9]{1,2}-[0-9]{1,2}
没有组。事实上,当您尝试通过循环中的
m[i]
索引匹配对象时,它应该会引发运行时错误。
if ( regex_search( start, end, m, rx ) )
{
    for ( int i = 0; i < 12; i++ )
    {
         std::string str(m[i].first, m[i].second);
         cout << "group " << i << " = " << str << "\r\n";
    }
 }