C ++ 11正则表达式可对数学表达式进行标记化 [英] C++11 regex to tokenize Mathematical Expression

查看:58
本文介绍了C ++ 11正则表达式可对数学表达式进行标记化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来标记格式为字符串的字符串:(1 + 2)/(((8))-(100 * 34):

I have the following code to tokenize a string of the format: (1+2)/((8))-(100*34):

如果用户使用不属于我的正则表达式的运算符或字符,我想向用户抛出错误.例如,如果用户输入3 ^ 4或x-6

I'd like to throw an error to the user if they use an operator or character that isn't part of my regex. e.g if user enters 3^4 or x-6

是否可以否定我的正则表达式,进行搜索,如果是真的,则抛出错误?

Is there a way to negate my regex, search for it and if it is true throw the error?

可以改善正则表达式的表达吗?

Can the regex expression be improved?

  //Using c++11 regex to tokenize input string
  //[0-9]+ = 1 or many digits
  //Or [\\-\\+\\\\\(\\)\\/\\*] = "-" or "+" or "/" or "*" or "(" or ")"
  std::regex e ( "[0-9]+|[\\-\\+\\\\\(\\)\\/\\*]");
  std::sregex_iterator rend;
  std::sregex_iterator a( infixExpression.begin(), infixExpression.end(), e );

  queue<string> infixQueue;
  while (a!=rend) {
      infixQueue.push(a->str());
      ++a;
  }
  return infixQueue;

-谢谢

推荐答案

您可以使用定义的搜索表达式 [^ 0-9()+ \-*/] 对字符串进行搜索作为C ++字符串,作为"[^ 0-9()+ \\-*/]" ),星号或斜杠.

You can run a search on the string using the search expression [^0-9()+\-*/] defined as C++ string as "[^0-9()+\\-*/]" which finds any character which is NOT a digit, a round bracket, a plus or minus sign (in real hyphen), an asterisk or a slash.

使用此正则表达式搜索字符串进行的搜索不应返回任何内容,否则该字符串包含不受支持的字符,例如 ^ x .

The search with this regular expression search string should not return anything otherwise the string contains a not supported character like ^ or x.

[ ... ] 是肯定的字符类,这意味着找到一个字符,该字符是方括号中的字符之一.

[...] is a positive character class which means find a character being one of the characters in the square brackets.

[^ ... ] 是否定字符类,这意味着找到一个不是方括号中的字符之一的字符.

[^...] is a negative character class which means find a character NOT being one of the characters in the square brackets.

必须在方括号中转义才能被解释为文字字符的唯一字符是] \ -,其中-如果在方括号内的字符列表中是第一个或最后一个字符,则不得转义.但是,最好始终在方括号内转义-,因为这使正则表达式引擎/函数更容易检测到连字符应解释为文字字符,而不是"FROM x到z".

The only characters which must be escaped within square brackets to be interpreted as literal character are ], \ and - whereby - must not be escaped if being first or last character in the list of characters within the square brackets. But it is nevertheless better to escape - always within square brackets as this makes it easier for the regular expression engine / function to detect that the hyphen character should be interpreted as literal character and not with meaning "FROM x to z".

当然,此表达式不会检查缺少的右方括号.但是,与不需要使用基于输入的公式计算值的编译器或脚本解释器相比,公式分析器通常不需要与每个编译器相比,每个括号都必须有一个括号.

Of course this expression does not check for missing closing round brackets. But formula parsers do often not require that there is always a closing parenthesis for every opening parenthesis in comparison to a compiler or script interpreter simply because not needed to calculate the value based on entered formula.

这篇关于C ++ 11正则表达式可对数学表达式进行标记化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆