Regex for math expression

2019-09-01 10:56发布

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 = "[)]";

标签: c++ regex math
0条回答
登录 后发表回答