0

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 = "[)]";
chj
  • 91
  • 9
  • 4
    This is not possible. Check http://stackoverflow.com/questions/133601/can-regular-expressions-be-used-to-match-nested-patterns – Amnon Mar 13 '14 at 16:53
  • Hi, thanks for your reply. I am actually not looking for matching the parenthesis. It is more of matching a open round bracket must alway be before a rxstrIdentifier or rxstrConstant and a close round bracket must alway be after a rxstrIdentifier or rxstrConstant. I will have a separate method to do the matching actually. – chj Mar 13 '14 at 17:21
  • Ok, though according to your examples, brackets can also be before or after other brackets, not only rxstrIdentifier or rxstrConstant – Amnon Mar 13 '14 at 19:50

0 Answers0