Regex for math expression

梦想的初衷 提交于 2020-02-07 11:54:29

问题


I have some issue with C++ regex, and hope someone could offer some help. :)

Currently, base on the "rxAssign" as shown below, I am able to detect math expression that look like:

  1. x = x;
  2. x = a + 3 * 90 - b;
  3. x = 4; }
  4. x = a + 3 * 90 - b; }}

However, I need some changes to be able to accept open round bracket "(" and close round bracket ")" for math expression such as:

  1. x = (x);
  2. x = a + ((3 * 90) - b);
  3. x = 4; }
  4. x = (((a + 3) * 90) - b); }}

Is there anyway I can edit my current implementation to accept open/close round bracket? There might still be expression with no round bracket. I do have rxOpen and rxClose which represent open and close round bracket as well but not sure how to insert it in.

rxAssign = regex("^\\s*(" + rxstrIdentifier + ")\\s*=\\s*((" + rxstrRef + ")((\\s*([\-+*])\\s*(" + rxstrRef + "))*))\\s*[;]\\s*([} ])*\\s*$");

where

string rxstrIdentifier = "\\b[a-zA-Z]\\w*\\b";
string rxstrConstant = "\\b\\d+\\b";
string rxstrRef = "(?:" + rxstrIdentifier + ")|(?:" + rxstrConstant + ")"; // identifier or constant
string rxOpen = "[(]";
string rxClose = "[)]";

来源:https://stackoverflow.com/questions/22385395/regex-for-math-expression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!