Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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++ C++;std::regex。用正则表达式搜索子字符串时出错_C++_Regex_Visual Studio 2012_C++11 - Fatal编程技术网

C++ C++;std::regex。用正则表达式搜索子字符串时出错

C++ C++;std::regex。用正则表达式搜索子字符串时出错,c++,regex,visual-studio-2012,c++11,C++,Regex,Visual Studio 2012,C++11,更新:实际上删除了所有不必要的代码 我正在使用Visual Studio Express 2012 我编写了一个函数,用正则表达式在字符串中搜索子字符串,并用索引(i1、i2、i16…)替换它。它工作正常,但在字符串中不再有匹配的子字符串后,我得到错误: 表达式:字符串迭代器不可取消引用 我这样匹配: std::smatch m; std::regex rExp("[a-z][a-z0-9]*"); strBuf = "*10.5)*51E-10"; while (std::regex_sear

更新:实际上删除了所有不必要的代码

我正在使用Visual Studio Express 2012

我编写了一个函数,用正则表达式在字符串中搜索子字符串,并用索引(i1、i2、i16…)替换它。它工作正常,但在字符串中不再有匹配的子字符串后,我得到错误:

表达式:字符串迭代器不可取消引用

我这样匹配:

std::smatch m;
std::regex rExp("[a-z][a-z0-9]*");
strBuf = "*10.5)*51E-10";
while (std::regex_search (strBuf, m, rExp)) {...
在main()中,它工作正常,但当它在函数中运行时,会弹出错误消息

完整代码:

    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <regex>
    #include <fstream>

    std::string IntToString(int a) {
       std::string result;
       int number = abs(a);
       while (number) {
          result += (number % 10) + 48;
          number /= 10;
       }
       if (a < 0) result += '-';
       std::reverse(result.begin(), result.end());
       return result;
    }

    void searchLexemes(std::string& input, int& i)
    {
            std::string strBuf;
            std::smatch m;
            std::regex rExp("[a-z][a-z0-9]*");
            strBuf = input;
            input = "";
            bool foundAny = false;
            while (std::regex_search (strBuf, m, rExp)) { //here error happens
                    auto x = m.str();
                    input += m.prefix();
                    input += "i" + IntToString(i);
                    strBuf = m.suffix();
                    foundAny = true;
                    i++;
            }
            foundAny ? input += m.suffix() : input = strBuf;
    }

    void main() {
            std::string input;
            input = "fgs1=(b+a*5.01E+10)+a*10+(c*10.5)*51E-10";

            std::ofstream fout;
            fout.open("OUTPUT.TXT");

            int i = 1;

            searchLexemes(input, i);

            std::cout << input << std::endl;
            //expected result - "i1=(i2+i3*i5)+i3*i8+(i4*i7)*i6"

            system ("pause");
    }
#包括“stdafx.h”
#包括
#包括
#包括
#包括
std::string IntToString(inta){
std::字符串结果;
整数=abs(a);
while(数字){
结果+=(数字%10)+48;
数目/=10;
}
如果(a<0)结果+='-';
std::reverse(result.begin(),result.end());
返回结果;
}
void searchLexemes(std::string&input,int&i)
{
std::字符串strBuf;
std::smatch m;
标准::正则表达式rExp(“[a-z][a-z0-9]*”);
strBuf=输入;
输入=”;
bool-foundAny=false;
而(std::regex_search(strBuf,m,rExp)){//这里发生错误
自动x=m.str();
输入+=m.前缀();
输入+=“i”+输入字符串(i);
strBuf=m.后缀();
foundAny=true;
i++;
}
foundAny?输入+=m.后缀():输入=strBuf;
}
void main(){
std::字符串输入;
input=“fgs1=(b+a*5.01E+10)+a*10+(c*10.5)*51E-10”;
标准:流式流量计;
fout.open(“OUTPUT.TXT”);
int i=1;
搜索词素(输入,i);

std::cout您正在使用哪种编译器?std::regex仅在GCC 4.9.0之后才可用,并且在该版本之前不做任何操作。Visual Studio 2012.std::regex正在工作。“Visual Studio 2012.std::regex正在工作”。或者(12行程序)触发不可取消引用的迭代器异常。这里没有失败的地方,但它失败了。我怀疑std::regex在VS2012中工作得不太好。我不确定std::smatch是什么,但如果这里的字符串长于1个字母,则此代码正在工作,看起来m以某种方式与std关联。