Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.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/4/regex/18.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/0/xml/14.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++_Regex_Visual C++ - Fatal编程技术网

C++ 正则表达式匹配数学表达式

C++ 正则表达式匹配数学表达式,c++,regex,visual-c++,C++,Regex,Visual C++,我有一个正则表达式,我打算用它来“标记”一个数学表达式,如: a + b + 1 + 2 int main() { string rxstrIdentifier = "\\b[a-zA-Z]\\w*\\b"; string rxstrConstant = "\\b\\d+\\b"; string rxstrRef = "(" + rxstrIdentifier + ")|(" + rxstrConstant + ")"; // identifier or constan

我有一个正则表达式,我打算用它来“标记”一个数学表达式,如:

a + b + 1 + 2

int main() {
    string rxstrIdentifier = "\\b[a-zA-Z]\\w*\\b";
    string rxstrConstant = "\\b\\d+\\b";
    string rxstrRef = "(" + rxstrIdentifier + ")|(" + rxstrConstant + ")"; // identifier or constant

    const regex rxExpr = regex("^(" + rxstrRef + ")(.*)$"); // {x} [{+} {y}]*
    //const regex rxSubExpr = regex("^\\s*([+])\\s*(" + rxstrRef + ")(.*)$"); // {+} {x} [...]

    string test = "b + a + 1";
    cmatch res;
    regex_search(test.c_str(), res, rxExpr);
    cout << "operand: " << res[1] << endl;
    cout << "res: " << res[2] << endl;

    system("pause");
    return 0;
}
曾在另一个类似的正则表达式中工作

const regex Parser::rxExpr = regex("^(\\w+)((\\s*([+])\\s*(\\w+))*)$"); // {x} [{+} {y}]*
const regex Parser::rxSubExpr = regex("^\\s*([+])\\s*(\\w+)(.*)$"); // {+} {x} [...]

您的正则表达式似乎不允许字符串中有空格<代码>\b匹配单词边界,但边界的宽度为零,因此没有任何东西占用标记之间的空格。

使用(?:模式)组:


这消除了对搜索结果的影响

我建议实现一个真正的解析器,而不是一组正则表达式。如果您进一步扩展您的项目,那么维护一个真正的解析器将不会那么痛苦。这是我的建议,但我觉得团队不喜欢它。这是一个学校项目。。。因此,我认为现在也可以尝试使用正则表达式……您尝试标记的字符串的复杂性是否会增加?因为如果是这样的话,那么尽早地移动到解析器。复杂的正则表达式往往不是快速的或可维护的;有很多lexer和parser生成器支持这两种功能。。。我打算在下一部分中使用Boost::Spirit,增加复杂性
const regex Parser::rxExpr = regex("^(\\w+)((\\s*([+])\\s*(\\w+))*)$"); // {x} [{+} {y}]*
const regex Parser::rxSubExpr = regex("^\\s*([+])\\s*(\\w+)(.*)$"); // {+} {x} [...]
string rxstrRef = "(?:" + rxstrIdentifier + ")|(?:" + rxstrConstant + ")"; // identifier or constant