简体   繁体   English

用于验证数学公式的正则表达式

[英]Regular expression to validate a mathematical formula

I need to validate a string using regex to confirm whether it is following a valid format.我需要使用正则表达式验证字符串以确认它是否遵循有效格式。 The string can contain numbers, operators, space, dot, left parenthesis, right parenthesis, comma, these aggregate functions SUM, MAX, MIN, AVG and variables starting with letter V.字符串可以包含数字、运算符、空格、点、左括号、右括号、逗号、这些聚合函数 SUM、MAX、MIN、AVG 和以字母 V 开头的变量。

I found this regex ^[0-9+ -/()., ]+$ this checks 0-9 (numbers);我发现这个正则表达式 ^[0-9+ -/()., ]+$ 这检查 0-9 (数字); '+'; '+'; '-'; '-'; ' '; ' '; '/'; '/'; '('; ')'; '('; ')'; '.'; '.'; ','; ','; ' '(space). ' '(空间)。 But I am not able to include aggregate functions and letter V in this.但我无法在其中包含聚合函数和字母 V。

Some of the valid input strings are一些有效的输入字符串是

  1. AVG(SUM(1, 2, 3), SUM(4, 5, 6)) * 100
  2. SUM(V1/2,(2+7),3)+(V1+V2)

Can someone please help me on this.有人可以帮我解决这个问题。

From the comments on the question:从对该问题的评论中:

Are you trying to ensure that only valid characters, aggregate functions, and variable names appear in the string or are you attempting to also check that the string is well formatted (ie there is an operand on either side of an operator, parenthesis are matched, etc...)?您是否尝试确保字符串中仅出现有效字符、聚合函数和变量名,或者您是否还尝试检查字符串的格式是否正确(即运算符的任一侧都有一个操作数,括号匹配, ETC...)?

- DM - DM

@DMI am just trying to validate only for valid characters @DMI 只是试图仅验证有效字符

- DevMJ - DevMJ


Since you're only looking to check that a formula contains digits, functions, variables, etc (and not that it is also valid for execution), you can add possibilities as alternatives in one group.由于您只是想检查一个公式是否包含数字、函数、变量等(而不是它是否也可以执行),因此您可以在一组中添加可能性作为替代方案。

One possibility is the pattern ^(?:\d|\+|\-|\/|\*|\(|\)|\.|\,|AVG|SUM|MAX|MIN|V\d+| )*$ which matches the samples you provided.一种可能性是模式^(?:\d|\+|\-|\/|\*|\(|\)|\.|\,|AVG|SUM|MAX|MIN|V\d+| )*$与您提供的样本相匹配。

Try it out!试试看!

Explanation:解释:

Token令牌 Matches火柴
^ Start of a line一行的开始
(?: Start of the non-capturing group of alternatives非捕获组备选方案的开始
\d A digit (equivalent to [0-9] )一个数字(相当于[0-9]
\+ The + character +字符
\- The - character -字符
\/ The / character /字符
\* The * character *字符
\( The ( character (字符
\) The ) character )字符
\. The . . character特点
\, The , character ,字符
AVG The string AVG字符串AVG
SUM The string SUM字符串SUM
MAX The string MAX字符串MAX
MIN The string MIN字符串MIN
V\d+ The V character followed by one or more digits V字符后跟一个或多个数字
A space空间
) End of the non-capturing group of alternatives非捕获组的结束
* Any of the alternatives zero or more times任何替代方案零次或多次
$ End of a line一行结束

As mentioned in the comments, if you also want to check that the string can be executed successfully, you will need to look into defining a context-free grammar for your "language" and using a tool like ANTLR to parse strings using the grammar.正如评论中提到的,如果您还想检查字符串是否可以成功执行,您需要考虑为您的“语言”定义一个上下文无关的语法,并使用像ANTLR这样的工具来使用语法解析字符串。

Since all you care for is the valid characters, that's indeed a job for regexes.由于您只关心有效字符,因此这确实是正则表达式的工作。

A simple way to filter this is just to add letters to the valid characters:一种简单的过滤方法就是在有效字符中添加字母:

^[A-Z0-9+-/()., ]+$

You can even add az if you want to allow lowercase characters as well.如果您还想允许小写字符,您甚至可以添加az

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM